将WordPress类别显示为分层

我正在尝试使用以下代码打印与帖子相关的类别。默认情况下, 它似乎按字母顺序排序。有没有一种方法可以按类别层次结构对其进行排序?

<footer class="categories-links">
<?php $post_category = get_the_category($post->ID); if ( $post_category==true 
) { ?>  
<span > <?php the_category(' ') ?></span> <?php }?>
</footer>

当前, 例如, 我有:父类别:水果, 子类别:树木, 子子类别:苹果,

它以苹果-水果-树木的形式打印出来。而它必须是水果-树木-苹果


#1


你可以试试看这是来自Stackexchange。但这是获得的完美方法。你还可以在类别中传递$ args。欲了解更多信息

  hierarchical_category_tree( 0 ); // the function call; 0 for all categories; or cat ID  

function hierarchical_category_tree( $cat ) {
    // wpse-41548 // alchymyth // a hierarchical list of all categories //

  $next = get_categories('hide_empty=false&orderby=name&order=ASC&parent=' . $cat);

  if( $next ) :    
    foreach( $next as $cat ) :
    echo '<ul><li><strong>' . $cat->name . '</strong>';
    echo ' / <a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>View ( '. $cat->count . ' posts )</a>  '; 
    echo ' / <a href="'. get_admin_url().'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$cat->term_id.'&post_type=post" title="Edit Category">Edit</a>'; 
    hierarchical_category_tree( $cat->term_id );
    endforeach;    
  endif;

  echo '</li></ul>'; echo "\n";
}  

来源:

https://www.srcmini02.com/65824.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_40771.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?