显示带有兄弟姐妹和当前类别帖子的Wordpress父类别

时间:2017-06-30 22:24:23

标签: wordpress navigation categories

这是我正在尝试做的一个例子: enter image description here

这是侧边栏导航。当访问者在子类别上时,我需要子类别来显示该类别下的当前帖子以及兄弟子类别。这种结构也必须在岗位层面工作。

当访问者处于类别级别时 - 我进行了设置,以便用户只能看到该类别下的子类别 - 这正是我所需要的。我只是想要整理子类别模板(它是自己的模板 - 与类别模板分开)。

如果有一个插件可以快速解决这个问题,我会全力以赴。现在我只是试图使用wp_list_categories而没有运气。我感谢你的帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

 <?php 
 $taxonomy_name = 'products-category';

$args = array(
    'type' => 'products',
    'orderby'          => 'name',
    'order'               => 'ASC',
    'taxonomy'       => $taxonomy_name,
    'parent' => '0',
    'hide_empty' => 0
); 

 $categories = get_categories( $args ); 


foreach($categories as $category){
echo   $category->name;
echo  $term_id =  $category->term_id.'<br>';


$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';

         // Note : Here is your Post loop using term id 


}
echo '</ul>';


}
?>

祝你好运

相关问题