如何自定义 wp_list_pages() 的输出,以便我可以将链接包装在我自己的 HTML 中



我是WordPress的新手,想创建一个菜单。有了wp_list_pages()我可以生成一个包裹在<li>标签中的链接列表。

这很好,但我想将我自己的类添加到这些类中(并且可能还会更改包装它们的元素(。

我已经浏览了 https://developer.wordpress.org/reference/functions/wp_list_pages/和一些论坛帖子,但找不到任何指向我正确方向的内容。

如何自定义wp_list_pages的输出?

我找到了两种方法,

使用过滤器和正则表达式

将以下内容添加到您的主题functions.php文件中。

function clean_wp_list_pages($menu) {
    // Remove redundant title attributes
    $menu = remove_title_attributes($menu);
    // Remove protocol and domain name from href values
    $menu = make_href_root_relative($menu);
    // Give the list items containing the current item or one of its ancestors a class name
    $menu = preg_replace('/class="(.*?)current_page(.*?)"/','class="sel"',$menu);
    // Remove all other class names
    $menu = preg_replace('/ class=(["'])(?!sel).*?1/','',$menu);
    // Give the current link and the links to its ancestors a class name and wrap their content in a strong element
    $menu = preg_replace('/class="sel"><a(.*?)>(.*?)</a>/','class="sel"><a$1 class="sel"><strong>$2</strong></a>',$menu);
    return $menu;
}
add_filter( 'wp_list_pages', 'clean_wp_list_pages' );

使用自定义助行器函数

它与上面相同。将此添加到您的函数.php文件中:

class Clean_Walker extends Walker_Page {
    function start_lvl(&$output, $depth) {
        $indent = str_repeat("t", $depth);
        $output .= "n$indent<ul>n";
    }
    function start_el(&$output, $page, $depth, $args, $current_page) {
        if ( $depth )
            $indent = str_repeat("t", $depth);
        else
            $indent = '';
        extract($args, EXTR_SKIP);
        $class_attr = '';
        if ( !empty($current_page) ) {
            $_current_page = get_page( $current_page );
            if ( (isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors)) || ( $page->ID == $current_page ) || ( $_current_page && $page->ID == $_current_page->post_parent ) ) {
                $class_attr = 'sel';
            }
        } elseif ( (is_single() || is_archive()) && ($page->ID == get_option('page_for_posts')) ) {
            $class_attr = 'sel';
        }
        if ( $class_attr != '' ) {
            $class_attr = ' class="' . $class_attr . '"';
            $link_before .= '<strong>';
            $link_after = '</strong>' . $link_after;
        }
        $output .= $indent . '<li' . $class_attr . '><a href="' . make_href_root_relative(get_page_link($page->ID)) . '"' . $class_attr . '>' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
        if ( !empty($show_date) ) {
            if ( 'modified' == $show_date )
                $time = $page->post_modified;
            else
                $time = $page->post_date;
            $output .= " " . mysql2date($date_format, $time);
        }
    }
}

要使用它,您需要使用 walker 参数调用 wp_list_pages。

<?php
$walker = new Clean_Walker();
wp_list_pages( array(
    'title_li' => '',
    'walker' => $walker,
    ) );
?>

来源:这里

您可以通过重写 .page-item 类来实现此目的。 查看此处了解更多详情 https://developer.wordpress.org/reference/functions/wp_list_pages/#more-information

如果需要添加自定义类或更改列表的呈现方式,则可以附加 walker 函数 https://developer.wordpress.org/reference/classes/walker/

可以在

https://developer.wordpress.org/reference/classes/walker_nav_menu/

class Custom_Walker_Page extends Walker_page {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
   //You can make your change here
}
}
$args = array(
.....
.....
'walker' => new Custom_Walker_page()

相关内容

最新更新