为 WordPress 中的不同类别页面更改小部件的 CSS 类名称 [英] Change widget's CSS class names for different category pages in WordPress

查看:27
本文介绍了为 WordPress 中的不同类别页面更改小部件的 CSS 类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WordPress 的每个不同类别页面上更改侧边栏小部件的类名,我认为最好的方法是在 functions.php 中创建一个具有所有条件的函数并返回所需的类名.然后我调用了 register_sidebar 函数的列表标签中的函数.

I want to change the class names of the sidebar widgets on every different category page of WordPress and I figured the best way to do this would be to make a function in functions.php with all the conditions and return the required class name. I then called the function in the list tags of the register_sidebar function.

if (function_exists('register_sidebar')) {
  register_sidebar(array(
   'before_widget' => '<li class="sidebarModule">',
   'after_widget' => '</li><!-- end module -->',
   'before_title' => '<h2 class="moduleTitle "'.set_widget_title_color().'>',
   'after_title' => '</h2>',
  ));
}


function set_widget_title_color() {

    if(is_category('technology')) {
        $color = "catNavColor1_active";
    } elseif(is_category('gadgets')) {
        $color = "catNavColor2_active";
    } elseif(is_category('social-media')) {
        $color = "catNavColor3_active";
    } elseif(is_category('gaming')) {
        $color = "catNavColor4_active";
    } elseif(is_category('other')) {
        $color = "catNavColor5_active";
    }

    return $color;
}

出于某种原因,上述方法不起作用.请帮忙

For some reason the above doesn't work. Please Help

谢谢

推荐答案

我认为 register_sidebars 在过程中被调用得太早了,当时还没有定义类别.您是否尝试过实施 dynamic_sidebar_params 过滤器?我在 WordPress Codex 中找不到任何内容,但我找到了这个 很好的例子.您关于此主题的其他问题也有完整答案.

I think register_sidebars is called too early in the process, when the category is not defined yet. Have you tried implementing the dynamic_sidebar_params filter? I could not find anything in the WordPress Codex, but I found this nice example. Your other question on this topic also has a complete answer.

这仅在您在小部件中使用 dynamic_sidebar 实现侧边栏时才有效,因为此函数调用 dynamic_sidebar_params 过滤器.如果您有静态小部件(在您的模板中定义,而不是使用小部件管理页面),您应该在该模板代码中添加对您的函数的调用,如下所示:

This only works if you implement the sidebars using dynamic_sidebar in your widget, as this function calls the dynamic_sidebar_params filter. If you have static widgets (defined in your template, not using the widget admin page), you should add a call to your function in that template code, like this:

<li class="sidebarModule">
<h2 class="moduleTitle <?php echo set_widget_title_color(); ?>">Widget title</h2>
<?php /* Your widget code */ ?>
</li><!-- end module -->

这篇关于为 WordPress 中的不同类别页面更改小部件的 CSS 类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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