0

我一直在尝试为当前父页面的每个子页面显示字幕。目前我已经让它工作了,所以它显示了子页面的所有标题。

基本上我希望它在子页面的每个标题下方显示副标题。

// Get childern
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') :  wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');

// Set subtitle      
$subtitle = get_the_title($post->the_subtitle);

echo $children;
echo $subtitle;

任何帮助都感激不尽。

干杯

4

1 回答 1

0

为此,您有两种选择:

1. OOP 方法

扩展Walker_page类以创建自定义迭代器以与wp_list_pages().

将此添加到您的functions.php

class Subtitle_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);
        $css_class = array('page_item', 'page-item-'.$page->ID);
        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';
            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';
        }

        $css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );

        //Added subtitle support
        $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . get_the_post_thumbnail($page->ID, array(72,72)) .'</a>'.' <span class="subtitle">'.get_the_subtitle($page->ID,'','',0).'</span>';

        if ( !empty($show_date) ) {
            if ( 'modified' == $show_date )
                $time = $page->post_modified;
            else
                $time = $page->post_date;

            $output .= " " . mysql2date($date_format, $time);
        }
    }
}

并在您的模板文件中调用它:

$subtitle_menu = new Subtitle_walker();
$args = array(
    'title_li' => '',
    'child_of' => ($post->post_parent) ? $post->post_parent : $post->ID,
    'echo'  => 0,
    'walker' => $subtitle_menu
);
$children =  wp_list_pages($args);
echo $children;

注意:前面的代码基于本教程,您可以阅读它以深入了解它的作用

2. 使用自定义 WP_Query 循环

您可以使用get_page_children查询子页面,然后遍历它们并构建/回显您的自定义列表项:

//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

//Get children
$children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) :  get_page_children( $post->ID, $all_wp_pages );

//Build custom items 
foreach($children as $child){
    echo '<li class="page-item">';
    echo '<a href="'.get_permalink($child->ID).'">'.get_the_title($child->ID).'</a><br/>';
    echo '<span class="subtitle">'.get_the_subtitle($child->ID).'</span>';
    echo '</li>';
}
于 2013-09-02T05:44:16.920 回答