Php 列出页面并显示字幕

Php 列出页面并显示字幕,php,wordpress,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='.

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

基本上,我希望它显示的字幕下面的每个标题的儿童网页

// 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.面向对象方法 扩展
Walker\u页面
类,创建一个自定义迭代器,用于
wp\u列表页面()

将此添加到
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);
        }
    }
}
注意:前面的代码是基于此编写的,您可以阅读它来深入解释它的功能

2.使用自定义WP_查询循环 您可以使用来查询子页面,然后在其中循环并生成/回显自定义列表项:

//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>';
}
//设置所需的对象
$my_wp_query=新wp_query();
$all_wp_pages=$my_wp_query->query(数组('post_type'=>'page');
//生孩子
$children=($post->post\u parent)?获取页面子项($post->post\u parent,$all\u wp\u pages):获取页面子项($post->ID,$all\u wp\u pages);
//生成自定义项
foreach($childrenas$child){
echo'
  • ; 回声“
    ”; 回显“”。获取子标题($child->ID)。“”; 回音“
  • ”; }
    您是如何获得字幕支持的?你安装了插件还是使用了自定义字段?我安装了一个名为WP Subtitle的插件。嗨,koala_dev,非常感谢你的回复。选项2,使用自定义WP_查询循环,是我想要的。非常感谢。蒂姆干杯
    //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>';
    }