WordPress:动态更改侧边栏小部件的标题

发布于 2024-11-08 23:04:05 字数 277 浏览 0 评论 0原文

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

例如: 'before_title'=>;

',

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

谢谢

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?

For example:
'before_title' => <h2 class="title of the widget">',

If I inspect the element, I want to see:
<h2 class="author"> and <h2 class="links"> etc...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫雪独思 2024-11-15 23:04:05

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

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

dynamic_sidebar_params 过滤器,传递的数组具有以下结构(我通过 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 数组。您可能还需要执行一些条件逻辑才能获得您想要的确切类,但我将把它留给您。

这是一个例子:

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');

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

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

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
        )

)

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.

Here is an example:

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文