我有什么 我有一个代码显示元发布时间、子类别最后一级的日期,
我想要什么 我希望所有类别级别应显示为主类别>>子类别级别#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
}
尝试这个代码。它会解决你的问题。
<?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>";
}
}
?>