将引导类/样式添加到 Wordpress wp_list_pages菜单项


Add Bootstrap class/style to Wordpress wp_list_pages menu items

我有一些代码可以在我的Wordpress网站上生成侧边栏菜单,如下所示...

<ul>
    <?php
    if($post->post_parent)
        $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->post_parent."&echo=1");
    else
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=1");
        if ($children) { ?>
            <?php echo $children; ?>
        </div>
    <?php } ?>
</ul>

这将按以下方式输出代码...

<ul>
    <li class="page_item page-item-94 current_page_item"><a href="#">Menu Item</a></li>
</ul>

问题是,我需要它来使用我的 Bootstrap 3 样式并输出列表,如下所示......

<div class="list-group submenu">
    <a href="#" class="list-group-item">Menu Item</a>
</div>

基本上我需要将列表组项的类添加到输出列表项中的 a 标签中。这可能吗?

你可以通过jquery做到这一点

$(document).ready(function(){
    $("ul").find("li").each(function(){
      $(this).addClass("YourClass");
    })
})

你可以你的沃克类简单地扩展输出wp_list_pages修改你的$output把类放在functions.php

class NS_CS_Walker extends Walker_page {
    function start_el( &$output, $page, $depth, $args, $current_page ) {
        if ( $depth ) {
            $indent = str_repeat( "'t", $depth );
        } else {
            $indent = '';
        }
        extract( $args, EXTR_SKIP );
        $output .= $indent . '<li><div>' . get_post_meta( $post_id, $key, $single ) . '</div></li>';
        // modify your output here
    } // End start_el
} // End Walker Class

那么你的渲染代码应该是

$args = array(
    'walker'      => new NS_CS_Walker()
        // your extra $args would be here
);
wp_list_pages( $args );