list child pages as slug

时间:2012-11-14 作者:jamie

我在我的网站上有一个部分,需要一个页面列表,这些页面是特定ID的后代。我需要将它们列在子页面和孙子页面上。但是页面标题太长,所以我需要使用slug作为标题。我想我可以只使用wp\\u list\\u页面并专门调用此ID的后代,但wp\\u list\\u页面似乎没有办法列出slug而不是页面标题。此外,我必须使用str\\u替换段塞中的破折号。有人能告诉我从哪里开始吗?我最终使用了下面的,这没有更好的效果。。。有什么见解吗?

function is_tree($pid) {      
global $post;         
$anc = get_post_ancestors( $post->ID );

foreach($newanc as $ancestor) 
{
    if(is_page() && $ancestor == $pid) {
    return true;
    }
}
if(is_page()&&(is_page($pid)))
返回true;//我们位于页面或子页面,否则返回false;//我们在别处};

2 个回复
SO网友:David Gard

好的,这段代码应该会让您走上正确的道路。它的设计目的并不是要完全满足您的需要,但从上面的代码来看,您似乎相当符合要求,因此应该能够根据需要进行修改。我还建议您看看Walker_Page 类,这样您就可以将下面的方法与Walker_Page, 并查看已更改的内容(wp includes/post-template.php)。

将此放入functions.php, 然后打电话的时候wp_list_pages($args), 确保包含参数\'walker\' => new Walker_Custom_Page.

<?
/**
 * Custom Walker for creating the page list.
 */
class Walker_Custom_Page extends Walker_Page {

    /**
     * @see Walker::start_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param int $depth Depth of page. Used for padding.
     */
    function start_lvl(&$output, $depth) {
        $indent = str_repeat("\\t", $depth);
        $output .= "\\n{$indent}<ul class=\'children\'>\\n";
    }

    /**
     * @see Walker::start_el()
     *
     * @param string $output Passed by reference. Used to append additional content
     * @param object $page Page data object
     * @param int $depth Depth of page. Used for padding
     * @param array $args
     * @param int $current_page Page ID
     */
    function start_el(&$output, $page, $depth, $args, $current_page) {

        /** Add an indent (if required) before the page */
        if($depth) :
            $indent = str_repeat("\\t", $depth);
        else :
            $indent = \'\';
        endif;

        /** Make \'new_href\' and \'image\' arrays to avoid errors if it is empty */
        if(!is_array($args[\'new_href\'])) : $args[\'new_href\'] = array($args[\'new_href\']); endif;

        /** Check to see if an alternitive link has been specified and set the $link_href **/
        if(array_key_exists((string)$page->ID, $args[\'new_href\'])) : 
            $link_href = $args[\'new_href\'][(string)$page->ID];
            $target = \'\';
        elseif(array_key_exists(\'_\'.(string)$page->ID, $args[\'new_href\'])) : 
            $link_href = $args[\'new_href\'][\'_\'.(string)$page->ID];
            $target = \' target="_blank"\';
        else :
            $link_href = get_permalink($page->ID);
            $target = \'\';
        endif;

        /** Make the link for the page */
        $page_children = get_pages(\'child_of=\'.$page->ID);
        $link_in = (in_array($page->ID, $args[\'no_link\'])) ? \'<a>\' : \'<a href="\'.$link_href.\'"\'.$target.\'>\';
        $link_out = \'</a>\';
        $link_after = (count($page_children) !== 0 && $depth !== 0) ? \' &raquo;\' : false;
        $link = $link_in.$link_before.apply_filters(\'the_title\', $page->post_title, $page->ID).$link_after.$link_out;

        /** Extract the args */
        extract($args, EXTR_SKIP);

        /** Make the CSS class for the link */
        $css_class = array(\'page_item\', \'page-item-\'.$page->ID);
        $current_page = apply_filters(\'change_current_page\', $current_page);
        if(!empty($current_page)) :

            $_current_page = get_page($current_page);
            _get_post_ancestors($_current_page);

            if(isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors)) :
                $css_class[] = \'current_page_ancestor\';
            endif;

            if($page->ID == $current_page) :
                $css_class[] = \'current_page_item\';
            elseif($_current_page && $page->ID == $_current_page->post_parent) :
                $css_class[] = \'current_page_parent\';
            endif;

        elseif($page->ID == get_option(\'page_for_posts\')) :
            $css_class[] = \'current_page_parent\';
        endif;

        /** Filter the page CSS class so that extra can be added */
        $css_id = $css_class[1];
        $css_class = implode(\' \', apply_filters( \'page_css_class\', $css_class, $page, $depth, $args, $current_page));

        /** Add the page link to $output */
        $output .= $indent . \'<li id="\'.$css_id.\'" class="\' . $css_class . \'">\' . $link;

        /** Add the time that the page was modified (if desired) */
        if(!empty($show_date)) :

            if(\'modified\' == $show_date) :
                $time = $page->post_modified;
            else :
                $time = $page->post_date;
            endif;

            $output .= \' \'.mysql2date($date_format, $time);

        endif;

    }

}
?>

SO网友:Stephen Harris

这更简洁一些:

add_filter(\'the_title\',\'wpse72581_use_slug_not_title\',10,2);
wp_list_pages( ... );
remove_filter(\'the_title\',\'wpse72581_use_slug_not_title\',10,2);

function wpse72581_use_slug_not_title( $title, $post_id ){
    $post_obj = get_post( $post_id );
    $post_slug = esc_html($post_obj->post_name);
    return $post_slug;
}
我不知道你为什么喜欢使用slug:正如你所提到的,添加破折号和删除重音符号等方法可以消除这个问题。也许相反,删掉标题会更好?(参见wp_trim_words())

结束