Wp_list_ages中的空页上没有链接

时间:2017-05-03 作者:Emersion

目前我正在使用

<?php wp_list_pages( \'post_type=region&title_li=\' ); ?>
获取自定义帖子“区域”的层次结构列表。它工作得很好。我得到了这个:

欧洲、比利时、法国、亚洲、中国,每个项目都有链接。但有些页面没有内容。它只是用来创建层次结构的。在本例中,亚洲没有内容。

所以我想用一种方法创建这个菜单列表,但是如果页面没有内容->没有这个元素的链接。

所以我想,我需要使用wp\\u list\\u页面以外的另一种方式。但是哪一个呢?

你能帮帮我吗?

非常感谢。

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

wp_list_pages() 使用名为Walker_Page 渲染其输出,这可以被覆盖。

以下是的自定义版本Walker_Page::start_el(). 首先从核心复制代码,然后在输出链接之前添加一个简单的检查。如果一篇文章没有任何内容,那么标题将包装在<span> 标记而不是<a>.

此处突出显示修改:

    ...
    // Modification: If a posts content is empty, do not link the item. Instead use a span tag to wrap the text.
    $maybe_link_item = empty( $page->post_content ) ? \'<li class="%s"><span class="no-link">%3$s%4$s%5$s</span>\' : \'<li class="%s"><a href="%s">%s%s%s</a>\';

    $output .= $indent . sprintf(
        $maybe_link_item, // Modification: Using this variable instead of \'<li class="%s"><a href="%s">%s%s%s</a>\'
        $css_classes,
        get_permalink( $page->ID ),
        $args[\'link_before\'],
        /** This filter is documented in wp-includes/post-template.php */
        apply_filters( \'the_title\', $page->post_title, $page->ID ),
        $args[\'link_after\']
    );
    ...
这是完整的步行者:

class WPSE_Walker_Page extends Walker_Page {
    // Copied from Walker_Page::start_el() core version v4.7.4.
    /**
     * Outputs the beginning of the current element in the tree.
     *
     * @see Walker::start_el()
     * @since 2.1.0
     * @access public
     *
     * @param string  $output       Used to append additional content. Passed by reference.
     * @param WP_Post $page         Page data object.
     * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
     * @param array   $args         Optional. Array of arguments. Default empty array.
     * @param int     $current_page Optional. Page ID. Default 0.
     */
    public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
        if ( isset( $args[\'item_spacing\'] ) && \'preserve\' === $args[\'item_spacing\'] ) {
            $t = "\\t";
            $n = "\\n";
        } else {
            $t = \'\';
            $n = \'\';
        }
        if ( $depth ) {
            $indent = str_repeat( $t, $depth );
        } else {
            $indent = \'\';
        }

        $css_class = array( \'page_item\', \'page-item-\' . $page->ID );

        if ( isset( $args[\'pages_with_children\'][ $page->ID ] ) ) {
            $css_class[] = \'page_item_has_children\';
        }

        if ( ! empty( $current_page ) ) {
            $_current_page = get_post( $current_page );
            if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
                $css_class[] = \'current_page_ancestor\';
            }
            if ( $page->ID == $current_page ) {
                $css_class[] = \'current_page_item\';
            } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
                $css_class[] = \'current_page_parent\';
            }
        } elseif ( $page->ID == get_option(\'page_for_posts\') ) {
            $css_class[] = \'current_page_parent\';
        }

        /**
         * Filters the list of CSS classes to include with each page item in the list.
         *
         * @since 2.8.0
         *
         * @see wp_list_pages()
         *
         * @param array   $css_class    An array of CSS classes to be applied
         *                              to each list item.
         * @param WP_Post $page         Page data object.
         * @param int     $depth        Depth of page, used for padding.
         * @param array   $args         An array of arguments.
         * @param int     $current_page ID of the current page.
         */
        $css_classes = implode( \' \', apply_filters( \'page_css_class\', $css_class, $page, $depth, $args, $current_page ) );

        if ( \'\' === $page->post_title ) {
            /* translators: %d: ID of a post */
            $page->post_title = sprintf( __( \'#%d (no title)\' ), $page->ID );
        }

        $args[\'link_before\'] = empty( $args[\'link_before\'] ) ? \'\' : $args[\'link_before\'];
        $args[\'link_after\'] = empty( $args[\'link_after\'] ) ? \'\' : $args[\'link_after\'];

        // Modification: If a posts content is empty, do not link the item. Instead use a span tag to wrap the text.
        $maybe_link_item = empty( $page->post_content ) ? \'<li class="%s"><span class="no-link">%3$s%4$s%5$s</span>\' : \'<li class="%s"><a href="%s">%s%s%s</a>\';

        $output .= $indent . sprintf(
            $maybe_link_item, // Modification: Using this variable instead of \'<li class="%s"><a href="%s">%s%s%s</a>\'
            $css_classes,
            get_permalink( $page->ID ),
            $args[\'link_before\'],
            /** This filter is documented in wp-includes/post-template.php */
            apply_filters( \'the_title\', $page->post_title, $page->ID ),
            $args[\'link_after\']
        );

        if ( ! empty( $args[\'show_date\'] ) ) {
            if ( \'modified\' == $args[\'show_date\'] ) {
                $time = $page->post_modified;
            } else {
                $time = $page->post_date;
            }

            $date_format = empty( $args[\'date_format\'] ) ? \'\' : $args[\'date_format\'];
            $output .= " " . mysql2date( $date_format, $time );
        }
    }
}
要使用新的walker,请将其添加到主题functions.php 或插件,然后修改对wp_list_pages() 因此它使用了我们新的定制助行器:

    wp_list_pages( [
        \'post_type\' => \'region\',
        \'walker\'    => new WPSE_Walker_Page(),
        \'title_li\'  => \'\',
    ] );