显示帖子中的所有类别级别

时间:2017-02-05 05:52:15

标签: php wordpress post categories meta

我有什么 我有一个代码显示Meta帖子时间,日期子类别的最后一级,

我想要什么 我希望所有类别级别都应该像主要类别一样显示>>子类别等级#1>>子类别等级#2>> 像那样 例如:工作>>会计>>亚特兰大>>

我的代码是

<p class="post-meta">
    <span ><?php echo $category[01]->cat_name; ?> <?php     echo(get_post_meta($post->ID, 'cp_city', true)); ?> >> <?php if ( $post-  >post_type == 'post' ) the_category( ', ' ); else echo get_the_term_list( $post-  >ID, APP_TAX_CAT, '', ', ', '' ); ?>  »     <?php echo(get_post_meta($post->ID,   'cp_brand', true)); ?> >>  <?php echo(get_post_meta($post->ID, 'cp_year',   true)); ?></span> <span class="dashicons-before clock"><span><?php echo      appthemes_date_posted( $post->post_date ); ?></span></span>

</p>
<?php
}

1 个答案:

答案 0 :(得分:0)

试试这段代码。它会解决你的问题。

      <?php
        $post_categories = get_the_category(get_the_id());
        foreach ($post_categories as $post_category) {
            if( $post_category->parent == 0 ){
                //$parent_cat_link, $parent_cat_name contain base category link and name
                $parent_cat_link = get_category_link( $post_category->cat_ID );
                $parent_cat_name = get_the_category_by_ID( $post_category->cat_ID );
                //child_of attribute is your parent category id that will give you its child category 
                $cats = get_terms( array( 'taxonomy' => 'category', 'child_of' => $post_category->cat_ID ) );
                echo '<a href="'. esc_url( $parent_cat_link ) .'">' . esc_html( $parent_cat_name ). '</a> >>';
                foreach ( $cats as $key => $cat ) {
                    $cat_link = get_category_link( $cat->term_id );
                    $cat_name = get_the_category_by_ID( $cat->term_id );
                    echo '<a href="'. esc_url( $cat_link ) .'">'. esc_html( $cat_name ). '</a>>>';
                }
                echo "<br>";
            }
        }
        ?>