使用GET_CATEGORIES时突出显示当前类别

时间:2014-05-02 作者:Arvid Eriksson

我成功地使用了helgatheviking在这篇文章中的回答:Show children of top level category only 请参见下面的代码:

// get the category object for the current category
$thisCat = get_category( get_query_var( \'cat\' ) ); 

// if not top-level, track it up the chain to find its ancestor
while ( intval($thisCat->parent) > 0 ) {
    $thisCat = get_category ( $thisCat->parent );
}

//by now $thisCat will be the top-level category, proceed as before
$args=array(
    \'child_of\' => $thisCat->term_id,
    \'hide_empty\' => 0,
    \'orderby\' => \'name\',
    \'order\' => \'ASC\'
);
$categories=get_categories( $args );
foreach( $categories as $category ) { 
    echo \'<a href="\' . get_category_link( $category->term_id ) . \'" title="\'     . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' .     $category->name.\'</a>\';  } 
?>
然而,在这个子类别列表中,我需要突出显示当前类别。我相信有一个简单的答案,但我自己也弄不明白。

2 个回复
SO网友:Arvid Eriksson

我已经解决了我的问题。我在旧代码之前添加了此定义:

<?php
// get the category object for the current category
$thisTrueCat = get_category( get_query_var( \'cat\' ) ); 
print $thisTrueCat->term_id;
?>
以上$thisTrueCat, 不同于$thisCat 在已经存在的代码中,检索当前类别的ID,而不考虑其父类别。$thisCat 另一方面,设置为默认返回到当前类别父级的ID。

具有$thisTrueCat 建立后,我在循环中添加了以下代码:

<li <?php if ($thisTrueCat->term_id == $category->term_id) { ?> class="xshown" <?php } ?>
如果显示的当前类别与列表项的类别匹配,则这将为列表项提供类xshown。换句话说,使用CSS,我突出显示当前显示的类别。

以下是我的新代码的全部内容:

    <!-- call ID of thisTrueCat -->
<?php
// get the category object for the current category
$thisTrueCat = get_category( get_query_var( \'cat\' ) ); 
print $thisTrueCat->term_id; ?>

<ul class="submenu">
<!-- call submenu -->
<?php
    // get the category object for the current category
    $thisCat = get_category( get_query_var( \'cat\' ) ); 

    // if not top-level, track it up the chain to find its ancestor
    while ( intval($thisCat->parent) > 0 ) {
        $thisCat = get_category ( $thisCat->parent );
    }

    //by now $thisCat will be the top-level category, proceed as before
    $args=array(
        \'child_of\' => $thisCat->term_id,
        \'hide_empty\' => 0,
        \'orderby\' => \'name\',
        \'order\' => \'ASC\'
    );

    $categories=get_categories( $args );
    foreach( $categories as $category ) { ?>
        <li <?php if ($thisTrueCat->term_id == $category->term_id) { ?> class="xshown" <?php } ?>><a href="<?php echo get_category_link( $category->term_id ) ?>" ><?php echo $category->name ?></a></li>  
    <?php
    }; 
?>
</ul>   

SO网友:mrwweb

特别是因为你把类别放在一个无序的列表中,似乎值得一提的是wp_list_categories() 默认情况下,在存档页上为当前类别提供一个类,并附带$current_category 特殊用例的参数,实际上只是get_categories().

我不知道您的代码的确切上下文,但很可能您可以将其替换为以下内容:

<?php
// get the category object for the current category
$thisCat = get_category( get_query_var( \'cat\' ) ); 

// if not top-level, track it up the chain to find its ancestor
while ( intval($thisCat->parent) > 0 ) {
    $thisCat = get_category( $thisCat->parent );
}
$args = array(
    \'child_of\' => $thisCat->term_id,
    \'hide_empty\' => 0,
    \'orderby\' => \'name\',
    \'order\' => \'ASC\',
    \'use_desc_for_title\' => 0
); ?>
<ul class="submenu">
    <?php echo wp_list_categories( $args ); ?>
</ul>
这将为您提供CSS类current-cat<li> 还有current-cat-parent 如果这适用的话(尽管在你的情况下,我不认为会出现这种情况)。

作为辅助功能the title attribute is mostly useless 因此,最好将其关闭(如上面的代码所示),而不要将其包含在主题代码中。WordPress core is slowly moving toward doing that itself.

结束