列出页面并显示字幕

时间:2013-08-30 00:00:52

标签: php wordpress

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

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

// 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;

非常感谢任何帮助。

干杯

1 个答案:

答案 0 :(得分: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;

注意:以前的代码基于this tutorial,您可以阅读它以深入解释它正在做什么

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>';
}