Wordpress:动态更改侧边栏小部件的标题 [英] Wordpress : Change title of sidebar widget dynamically

查看:23
本文介绍了Wordpress:动态更改侧边栏小部件的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取我在侧边栏中启用的小部件的标题,以便我可以将其作为类属性的名称插入?

Is it possible to get the title of the widgets that I have enabled in my sidebar so I can insert that as a name for my class attribute?

例如:'before_title' =><h2 class="小部件的标题">',

如果我检查元素,我想看到:

推荐答案

这绝对是可能的,但如果你想动态地做到这一点,你需要使用过滤器.

It's definitely possible, but if you want to do it dynamically, you'd need to use a filter.

dynamic_sidebar_params 过滤器应该适合您.如果您不熟悉过滤器的作用,基本上过滤器允许您在内容显示在屏幕上或保存在数据库中之前对其进行编辑.在此处查看更多信息:http://codex.wordpress.org/Plugin_API/Filter_Reference

The dynamic_sidebar_params filter should work for you. If you're unfamiliar with what filters do, basically a filter allows you to edit content before it's displayed on screen or saved in the database. See more here: http://codex.wordpress.org/Plugin_API/Filter_Reference

dynamic_sidebar_params 过滤器中,传递的数组具有以下结构(我是通过执行 print_r 得到的)

In the dynamic_sidebar_params filter, the passed array has the following structure (I got this from doing a print_r)

Array
(
    [0] => Array
        (
            [name] => Primary Widget Area
            [id] => primary-widget-area
            [description] => The primary widget area
            [before_widget] => <li id="search-2" class="widget-container widget_search">
            [after_widget] => </li>
            [before_title] => <h3 class="widget-title">
            [after_title] => </h3>
            [widget_id] => search-2
            [widget_name] => Search
        )

    [1] => Array
        (
            [number] => 2
        )

)

这意味着,您所要做的就是在返回之前编辑 $params 数组.您可能还需要执行一些条件逻辑才能获得所需的确切类,但我会将其留给您.

So that means, all you have to do is edit the $params array before returning it. You might have to do some conditional logic as well to get the exact class that you want, but I'll leave that up to you.

这是一个例子:

function my_edit_widget_func($params) {
    $params[0]['before_title'] = '<h3 class="' . $params[0]['widget_name'] . '">' ;
    return $params;
}
add_filter('dynamic_sidebar_params', 'my_edit_widget_func');

这篇关于Wordpress:动态更改侧边栏小部件的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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