在类别页面上显示同级类别

时间:2022-01-18 作者:Alex Osipov

我有一个类别页面,如何在此页面上显示此类别的兄弟类别?我知道如何显示子类别:

<ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
        <?php
            $term = get_queried_object();
            $children = get_terms( $term->taxonomy, array(
                    \'parent\'    => $term->term_id,
                    \'hide_empty\' => false
            ) );
            if ( $children ) {
                    foreach( $children as $subcat ){
                            echo \'<li class="category-thumbnail-item"><a class="category-thumbnail" href="\' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . \'">\';
                            echo \'<div class="category-thumbnail-image"><img src="\' . z_taxonomy_image_url($subcat->term_id, \'medium\'). \'" data-pin-nopin="true" alt="\'. $subcat->name .\' Sub Category Image"> </div>\';
                            echo \'<div class="category-title">\' . $subcat->name . \'</div></a></li>\';
                    }
            }
            ?>
         </ul>
现在我想知道如何显示兄弟姐妹。。。我应该在之后调用父ID和子ID吗?我想我需要以某种方式获得term\\u id,我确信我遗漏了一些东西:(

             <ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
             <?php
             $term_id = 10;
             $taxonomy_name = \'products\';
             $termchildren = get_term_children( $term_id, $taxonomy_name );

                 if ( $termchildren ) {
                         foreach ( $termchildren as $child ){
                                 echo \'<li class="category-thumbnail-item"><a class="category-thumbnail" href="\' . esc_url(get_term_link($child, $child->taxonomy)) . \'">\';
                                 echo \'<div class="category-thumbnail-image"><img src="\' . z_taxonomy_image_url($child->term_id, \'medium\'). \'" data-pin-nopin="true" alt="\'. $child->name .\' Sub Category Image"> </div>\';
                                 echo \'<div class="category-title">\' . $child->name . \'</div></a></li>\';
                         }
                 }
                 ?>
                </ul>
我假设,但我如何从这个列表中排除我所在的现有类别。希望有意义:)请帮助任何人?

UPDATE: 所以我找到了解决方案:

<ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
<?php
$term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
        \'parent\'    => $term->term_id,
        \'hide_empty\' => false
) );
if ( $children ) {
        foreach( $children as $subcat ){
                echo \'<li class="category-thumbnail-item"><a class="category-thumbnail" href="\' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . \'">\';
                echo \'<div class="category-thumbnail-image"><img src="\' . z_taxonomy_image_url($subcat->term_id, \'medium\'). \'" data-pin-nopin="true" alt="\'. $subcat->name .\' Sub Category Image"> </div>\';
                echo \'<div class="category-title">\' . $subcat->name . \'</div></a></li>\';
        }
}
?>
</ul>
<ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
 <?php
     $term = get_queried_object();
     $siblings = get_terms( $term->taxonomy, array(
         \'parent\'    => $term->parent,
         \'taxonomy\'   => $taxonomy_name,
         \'exclude\'    => array( $current_term->term_id ),
         \'hide_empty\' => false,
     ) );
     if ( $siblings ) {
             foreach( $siblings as $sibling ){
                     echo \'<li class="category-thumbnail-item"><a class="category-thumbnail" href="\' . esc_url(get_term_link($sibling, $sibling->taxonomy)) . \'">\';
                     echo \'<div class="category-thumbnail-image"><img src="\' . z_taxonomy_image_url($sibling->term_id, \'medium\'). \'" data-pin-nopin="true" alt="\'. $sibling->name .\' Sub Category Image"> </div>\';
                     echo \'<div class="category-title">\' . $sibling->name . \'</div></a></li>\';
             }
     }
     ?>
    </ul>
我可以在这里展示兄弟姐妹,但现在我的挑战是

排除我所在的现有类别

  • 并且不显示顶级父类别的兄弟姐妹
    1. 有趣!

    2 个回复
    最合适的回答,由SO网友:Sally CJ 整理而成

    我有一个类别页面,如何在此页面上显示此类别同级类别?我知道如何显示儿童类别

    我想我需要弄到term\\u id

    在术语存档页上,例如example.com/category/foo/ (对于默认值category 分类学),

    您可以使用get_queried_object() 检索所查询术语的术语对象(foo 在上面的示例URL中)。

    $term = get_queried_object();
    $term_id = $term->term_id;
    
    get_queried_object_id() 仅检索所查询术语的术语ID。

    $term_id = get_queried_object_id();
    
    现在,对于检索术语的同级,您可以使用get_terms() 就像你在检索学期的孩子时所做的那样。例如,对于被查询的术语(或者简单地说,当前术语),

    $taxonomy_name = \'products\';
    
    // Get the full term object/data.
    $current_term = get_queried_object();
    
    // Get the term\'s direct children.
    // **Use \'child_of\' instead of \'parent\' to retrieve direct and non-direct children.
    $children = get_terms( array(
        \'taxonomy\'   => $taxonomy_name,
        \'parent\'     => $current_term->term_id,
        \'hide_empty\' => false,
    ) );
    
    // Display the children.
    if ( ! empty( $children ) ) {
        echo "<h3>Children of $current_term->name</h3>";
        echo \'<ul>\';
    
        foreach ( $children as $child ) {
            echo "<li>$child->name</li>";
        }
    
        echo \'</ul>\';
    }
    
    // Get the term\'s direct siblings.
    $siblings = get_terms( array(
        \'taxonomy\'   => $taxonomy_name,
        \'parent\'     => $current_term->parent,
        \'exclude\'    => array( $current_term->term_id ),
        \'hide_empty\' => false,
    ) );
    
    // Display the siblings.
    if ( ! empty( $siblings ) ) {
        echo "<h3>Siblings of $current_term->name</h3>";
        echo \'<ul>\';
    
        foreach ( $siblings as $sibling ) {
            echo "<li>$sibling->name</li>";
        }
    
        echo \'</ul>\';
    }
    
    如果你想使用get_term_children(), 请注意,它检索所有直接子级和非直接子级(就像get_terms() 如果child_of ,函数返回一个术语数组IDs,因此在您的foreach, 您可以使用get_term() 获取完整术语对象/数据。

    $term_id = get_queried_object_id(); // or just use a specific term ID, if you want to
    $termchildren = get_term_children( $term_id, $taxonomy_name );
    
    foreach ( $termchildren as $child_id ) {
        $child = get_term( $child_id );
        // ... your code.
    }
    
    如何从此列表中排除我所在的现有类别

    如果您使用get_terms(), 您可以使用exclude arg,就像您可以在上面的示例中看到的一样。

    如果您使用get_term_children(), 然后您可以使用continue like so:(但这只是一个例子;你可以使用array_filter() 筛选$termchildren 如果需要,请在循环项目之前排列)

    foreach ( $termchildren as $child_id ) {
        if ( (int) $child_id === (int) $term_id ) {
            continue;
        }
    
        $child = get_term( $child_id );
        // ... your code.
    }
    

    SO网友:Alex Osipov

    <ul class="display-children-block slider-container-list sub-categories-block front-categories-block front-categories-scroll">
    <?php
    $term = get_queried_object();
    $children = get_terms( $term->taxonomy, array(
    \'parent\'    => $term->term_id,
    \'hide_empty\' => false
    ) );
    if ( $children ) {
    foreach( $children as $subcat ){
            echo \'<li class="category-thumbnail-item"><a class="category-thumbnail" href="\' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . \'">\';
            echo \'<div class="category-thumbnail-image"><img src="\' . z_taxonomy_image_url($subcat->term_id, \'medium\'). \'" data-pin-nopin="true" alt="\'. $subcat->name .\' Sub Category Image"> </div>\';
            echo \'<div class="category-title">\' . $subcat->name . \'</div></a></li>\';
    }
    }
    ?>
    </ul>
    <ul class="display-siblings-block slider-container-list sub-categories-block front-categories-block front-categories-scroll">
        <?php
    $term = get_queried_object();
    $siblings = get_terms( $term->taxonomy, array(
     \'parent\'    => $term->parent,
       \'taxonomy\'   => $taxonomy_name,
       \'exclude\'    => array( $term->term_id ),
       \'hide_empty\' => false,
    ) );
    
    if ( $siblings ) {
         foreach( $siblings as $sibling ){
                 echo \'<li class="category-thumbnail-item"><a class="category-thumbnail" href="\' . esc_url(get_term_link($sibling, $sibling->taxonomy)) . \'">\';
                 echo \'<div class="category-thumbnail-image"><img src="\' . z_taxonomy_image_url($sibling->term_id, \'medium\'). \'" data-pin-nopin="true" alt="\'. $sibling->name .\' Sub Category Image"> </div>\';
                 echo \'<div class="category-title">\' . $sibling->name . \'</div></a></li>\';
         }
    }
    ?>
    </ul>
    
    这就是我为这一切所做的一切,它是有效的!

    此外,此代码段还将类添加到:

    add_filter( \'body_class\', \'custom_cat_archiev_class\' );
    function custom_cat_archiev_class( $classes ) {
        if ( is_category() ) {
            $cat = get_queried_object();
            $ancestors = get_ancestors( $cat->term_id, \'category\', \'taxonomy\' );
            $classes[] = \'category-level-\' . ( count( $ancestors ) + 1 );
        }
        return $classes;
    }
    
    我正在使用css从顶级类别中删除同级。

    老实说,我认为我可以让它更干净,没有类和css。但我不知道怎么做!