显示当前类别和子项

时间:2018-06-28 作者:William Jerome

我正在建立一个非营利家庭暴力支持网站,并努力寻找一种方法,在侧边栏的页面中创建积极类别的家长和it儿童链接。

动态显示类别父级及其子级的理想解决方案是什么?

3 个回复
SO网友:Dharmishtha Patel
$current_cat_id = get_queried_object_id();
$kids = get_terms([
    \'taxonomy\' => get_queried_object()->taxonomy,
    \'parent\'   => $current_cat_id,
]);

<?php

$terms = get_terms([
    \'taxonomy\' => get_queried_object()->taxonomy,
    \'parent\'   => get_queried_object_id(),
]);

echo \'<div style="height: 200px; text-transform: uppercase; border:1px solid #666666; padding:10px; overflow-y: scroll;">
<ul>
foreach ( $terms as $term) {
    echo \'<li><a href="\' . get_term_link( $term ) . \'">\' . $term->name . \'</a></li>\';  
}
echo \'</ul>
<br />\';
SO网友:Amritosh pandey

是否要列出所有子类别,即当前类别中的子类别?

如果是,那么您可以使用以下简单的代码片段,帮助您列出当前帖子父类别中的所有子类别。

Code for Getting subcategories of current category

<ul id="all-sub-categories">

<?php
   $postcat = get_the_category( $post->ID );
   foreach ($postcat as $cat) {
      if ($cat->parent != 0) {
        echo "<li><a href=\'" . get_category_link( $cat->term_id ) . "\'>" . $cat->name . "</a></li>";
      }
   }
?>

</ul>
就是这样。

SO网友:dhirenpatel22

终于找到了解决方法。。。在侧栏模板中添加以下代码。您可以用自定义分类法替换“类别”分类法。

<?php
        // get Post Object outside
        $queried_object = get_queried_object();
        if ( $queried_object ) {
            $post_id = $queried_object->ID;
        }
        //Returns All Term Items for "my_taxonomy"
        $term_list = wp_get_post_terms($post_id, \'category\', array("fields" => "all"));
        $term_list_id = wp_get_post_terms($post_id, \'category\', array("fields" => "ids"));
        echo \'<ul>\';
        foreach($term_list as $term) {
            $has_parent = $term->parent;
            $has_childrens = get_terms( $term->taxonomy, array( \'parent\'    => $term->term_id, \'hide_empty\' => false) );
            // If it have parents...
            if ( $has_parent ) {
                // If parent category is already selected
                if(in_array($has_parent, $term_list_id)){
                    // Do nothing
                }else{
                    // If parent category is not selected
                    $parent_id = $term->parent;
                    $parent_term = get_term( $parent_id, \'category\' );
                    // Display Parent
                    echo \'<li><a href="\' . get_category_link( $parent_id ) . \'">\' . $parent_term->name.\'</a></li>\';
                    echo \'<ul>\';
                    // Display Children
                    $childrens = get_categories( array(\'child_of\' => $parent_id), \'category\' );
                    foreach($childrens as $children) {
                        echo \'<li><a href="\' . get_category_link( $children->term_id ) . \'">\' . $children->name.\'</a></li>\';
                    }
                    echo \'</ul>\';
                }
            }elseif ( $has_childrens ) {
                // If it have childrens...
                // display parent
                echo \'<li><a href="\' . get_category_link( $term->term_id ) . \'">\' . $term->name.\'</a></li>\';
                    $childrens = get_categories( array(\'child_of\' => $parent_id), \'category\' );
                    echo \'<ul>\';
                // Display children
                foreach($has_childrens as $has_children) {
                    echo \'<li><a href="\' . get_category_link( $has_children->term_id ) . \'">\' . $has_children->name.\'</a></li>\';
                }
                echo \'</ul>\';
            }else{
                // No Parent No Child
                echo \'<li><a href="\' . get_category_link( $term->term_id ) . \'">\' . $term->name.\'</a></li>\';
            }
        }
        echo \'</ul>\';
    ?>
希望这能奏效。。!!

结束

相关推荐

Editing a theme's templates

我已成功安装模板。我知道如何使用模块、文章和菜单创建。我不知道如何使用代码部分。如何更改css上的样式如何修改模板如何修改主页我已多次尝试在安装目录中查找文件夹,但未能找到。如何查找并更改它。