最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Why is it so hard to add a class to the <ul> tag in the sidebar widget?

programmeradmin0浏览0评论

I have created a sidebar:

function my_sidebar() {

    register_sidebar(
        array(
            'name'          => 'Blog Sidebar',
            'id'            => 'blog-sidebar',
            'description'   => 'Sidebar For Blog Page',
            'before_widget' => '<div class="widget">',
            'after_widget'  => '</div>',
            'before_title'  => '<h5 class="widget-title">',
            'after_title'   => '</h5>'
        )
    );
}

add_action( 'widgets_init', 'my_sidebar');

I just added the "recent posts" widget and want to add a class to the <ul> tag but I can't seem to figure out how to do it in a simple way. I just want the <ul> tag to change to <ul class="link-list">

I have created a sidebar:

function my_sidebar() {

    register_sidebar(
        array(
            'name'          => 'Blog Sidebar',
            'id'            => 'blog-sidebar',
            'description'   => 'Sidebar For Blog Page',
            'before_widget' => '<div class="widget">',
            'after_widget'  => '</div>',
            'before_title'  => '<h5 class="widget-title">',
            'after_title'   => '</h5>'
        )
    );
}

add_action( 'widgets_init', 'my_sidebar');

I just added the "recent posts" widget and want to add a class to the <ul> tag but I can't seem to figure out how to do it in a simple way. I just want the <ul> tag to change to <ul class="link-list">

Share Improve this question asked May 6, 2018 at 10:56 user8463989user8463989 5931 gold badge8 silver badges24 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The quickest and practical solution for you would be to add your custom class to the before_widget wrapper:

'before_widget' => '<div class="widget ul-link-list">'

Then use CSS rules accordingly:

.ul-link-list ul {
  /* Some CSS rules */
}

It's not possible to modify markup normally via actions/filters because markup is hardcoded in Wordpress \wp-includes\theme-compat\sidebar.php

So it's possible to change only via js, or php html output parsing

发布评论

评论列表(0)

  1. 暂无评论