PHP / Wordpress - 将属性添加到无序列表中的当前类别 [英] PHP/Wordpress - add attribute to current category in unordered list

查看:143
本文介绍了PHP / Wordpress - 将属性添加到无序列表中的当前类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WooCommerce购物车插件,并使用默认WooCommerce模板的文件覆盖编写了自己的主题,以便进行更多的控制。所以我从零开始创建了一个侧栏,列出了所有的产品类别。它很好用:

 < ul class =sidebar-list> 
<?php
$ all_categories = get_categories('taxonomy = product_cat& hide_empty = 0& hierarchical = 1');
$ b foreach($ all_categories as $ cat){
echo'< li>< a href ='。get_term_link($ cat-> slug,'product_cat')。 ><跨度>。 $ cat-> name。'< / span>< / a>';
}
?>
< / ul>

但是,上面的 foreach loop不会输出任何类型的当前类别属性(就像列表项上的一个类)。所以我试图编写一些抓取当前产品类别的PHP,并将其在foreach循环内与正在显示的类别进行比较,如果匹配,则将当前类添加到列表项目中。

 < ul class =sidebar-list> 
<?php
$ all_categories = get_categories('taxonomy = product_cat& hide_empty = 0& hierarchical = 1');

$ terms = get_the_terms($ post-> ID,'product_cat');

foreach($ terms as $ term){
$ product_cat = $ term-> term_id;
break;

$ b $ foreach($ all_categories as $ cat){
echo'< li class =';

if($ product_cat == $ cat-> id){
echocurrent;
}

echo'>< a href ='。get_term_link($ cat-> slug,'product_cat')。'>< span>'。 $ cat-> name。'< / span>< / a>';
}
?>
< / ul>

正如您从我这里收集的信息中发现的那样,它不起作用。
$ b

我知道我有一个问题,我甚至无法抓住 $ cat-> id ,因为如果我回显它自己出来,我什么也得不到。看来我所有的都是 $ cat->名称 $ cat-> slug 另外,我相信我的逻辑也是有缺陷的。有人能让我朝着正确的方向前进吗?

谢谢,谢谢!谢谢! 可以使用 wp_list_categories



仅在归档/分类页面上将CSS类 current-cat 添加到活动类别中:

 <?php 
$ args = array(
'taxonomy'=>'product_cat',
'hide_empty'= > 0,
'hierarchical'=> 1
);
wp_list_categories($ args);
?>

将CSS类 current-cat 添加到 get_the_category()返回一个结果:

  <?php 
$ category = get_the_category();
$ current_category_ID = isset($ category-> cat_ID)? $ category-> cat_ID:0;
$ args = array(
'taxonomy'=>'product_cat',
'hide_empty'=> 0,
'hierarchical'=> 1,
'current_category'=> $ current_category_ID
);
wp_list_categories($ args);
?>


I'm using the WooCommerce shopping cart plugin and have written my own theme with file overrides for the default WooCommerce templates, in order for more control. So I created a sidebar from scratch that lists out all the product categories. It works great:

<ul class="sidebar-list">
    <?php 
        $all_categories = get_categories( 'taxonomy=product_cat&hide_empty=0&hierarchical=1' );

        foreach ($all_categories as $cat) {
            echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'"><span>'. $cat->name .'</span></a>';
        }
    ?>
</ul>

But, the above foreach loop does not output any sort of "current category" attribute (like a class on the list item). So I've attempted to write some PHP that grabs the current product category and compares it inside the foreach loop to the category being displayed and if they match, add a "current" class to the list item.

<ul class="sidebar-list">
    <?php 
        $all_categories = get_categories( 'taxonomy=product_cat&hide_empty=0&hierarchical=1' );

        $terms = get_the_terms( $post->ID, 'product_cat' );

        foreach ($terms as $term) {
            $product_cat = $term->term_id;
            break;
        }

        foreach ($all_categories as $cat) {
            echo '<li class="';

            if ( $product_cat == $cat->id ) {
                echo "current";
            }   

            echo '"><a href="'. get_term_link($cat->slug, 'product_cat') .'"><span>'. $cat->name .'</span></a>';
        }
    ?>
</ul>

As you might gather from me posting this here, it does not work.

I know I have a problem in that I'm not even able to grab the $cat->id, because if I echo it out on its own I get nothing. Seems all I have access to is the $cat->name and $cat->slug.

And on top of that, I'm sure my logic is flawed too. Can someone get me headed in the right direction here?

Thank you, thank you, thank you!

解决方案

You can use wp_list_categories:

Adds CSS class current-cat to the active category only on archive/category pages:

<?php
    $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => 0,
        'hierarchical' => 1
    );
    wp_list_categories($args);
?>

Adds CSS class current-cat to the active category on all pages where get_the_category() returns a result:

<?php
    $category = get_the_category();
    $current_category_ID = isset($category->cat_ID) ? $category->cat_ID : 0;
    $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => 0,
        'hierarchical' => 1,
        'current_category' => $current_category_ID
    );
    wp_list_categories($args);
?>

这篇关于PHP / Wordpress - 将属性添加到无序列表中的当前类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆