首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress -自定义post类型,显示父类别和子类别

Wordpress -自定义post类型,显示父类别和子类别
EN

Stack Overflow用户
提问于 2014-07-08 10:38:52
回答 2查看 4.2K关注 0票数 0

因此,我对Wordpress相当陌生,但到目前为止,我已经能够自己解决大部分问题。

我有一个定制的post类型-让我们称之为Machines。在这些机器中,有一个叫做Machine_type的类别。比如说我做了一个叫做Scissor Lifts的机器类型,然后是另一个叫做electric scissor lifts的机器类型,它是scissor lifts的一个子类型。

我想在单个post页面上显示这个信息-所以在electric scissor lifts页面上,我想显示像机器一样的面包屑- Scissor Electric。但这几乎是不可能的!我尝试过使用许多不同的教程,但它们似乎总是显示Machines - - - Final machine name,而不是我想要显示的实际2类!

似乎有点疯狂,没有简单的内在呼吁类别和儿童类别!我在用这个:

代码语言:javascript
复制
<?php 
    $terms = get_the_terms( $post->ID , 'machine_type' ); 
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, 'machine_type' );
        if( is_wp_error( $term_link ) )
            continue;
        echo '<p>Machine Type: <a href="' . $term_link . '">' . $term->name . '</a></p>';
    } 
?>

但这显然只是将所有信息转储出去,无法很好地分离父类别和子类别。

编辑:

关于这个问题的更新:我终于找到了一些似乎解决了正确问题的代码:

代码语言:javascript
复制
 <?php 
// get top level terms
$parents = get_terms( 'machine_type', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'machine_type' );
// output top level cats and their children
foreach( $parents as $parent ):
// output parent name and link
echo '<a href="' . get_term_link( $parent ) . '">' . $parent->name . '</a>: ';
// initialize array to hold child links
$links = array();
foreach( $categories as $category ):
    if( $parent->term_id == $category->parent ):
        // put link in array
        $links[] = '<a href="' . get_term_link( $category ) . '">' . $category->name .      '</a>';
    endif;
endforeach;
// join and output links with separator
echo join( ', ', $links );
endforeach;

?> 

然而,它输出的内容并不完全正确。例如,在一台被划分为Boom电梯和铰接电动吊臂电梯的机器上,它现在显示的是:"Boom Lift :铰接式吊臂(电动)Scissor升降机: Spider- Lift :“

因此,它增加了另外两个类别,不适用于这台机器。有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2014-07-29 13:15:07

代码语言:javascript
复制
$wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC',  'parent' =>0));
foreach($wcatTerms as $wcatTerm) : 
    echo $wcatTerm->name;
    $wsubargs = array('hierarchical'=>1,'show_option_none'=>'','hide_empty'=>0,'parent'=>$wcatTerm->term_id,'taxonomy'=>'product_cat');
    $wsubcats = get_categories($wsubargs);
    foreach ($wsubcats as $wsc):
        echo $wsc->name;
    endforeach;
endforeach; 

使用这个,它显示了两个级别的类别名称和产品名称。

票数 3
EN

Stack Overflow用户

发布于 2014-07-29 12:23:03

这可能对你有帮助

代码语言:javascript
复制
<?php 

    /********************** List out the parent and child inside parent ***/
    $taxonomyName = "your_taxonomy";
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
    if ($term->parent == 1) {  
    wp_list_categories('taxonomy=your_taxonomy&depth=1&show_count=0
    &title_li=&child_of=' . $term->term_id);

    } else {

    wp_list_categories('taxonomy=your_taxonomy&show_count=0
    &title_li=&child_of=' . $term->parent); 
    }
    ?> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24629671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档