显示父页面标题和子页面标题和内容

时间:2014-05-24 18:05:18

标签: wordpress parent-child parent custom-post-type

我有一个带有页面属性的自定义帖子类型,因此我可以创建子页面。我正在尝试在h1中显示父页面的标题,然后让它循环显示子页面的内容。下面的代码几乎可以做到这一点,但它也在第一位输出子页面作为h1标题,所以我得到了子页面标题的重复。如何排除子页面并阻止它们显示在循环的第一部分?

非常感谢。

<?php

echo "<ul>";
if ( have_posts() ) {
while ( have_posts() ) {
    the_post();
    echo "<li><h1>".get_the_title()."</h1>";

    $args=array(
            'orderby' => 'menu_order',
            'order' => 'ASC',
             'post_parent' => $post->ID,
                 'post_type' => get_post_type( $post->ID ),
            'posts_per_page' => 10
    );

    $childpages = new WP_Query($args);

    if($childpages->post_count > 0) { /* display the children content  */
        echo "<ul>";
        while ($childpages->have_posts()) {
             $childpages->the_post();
             echo "<li><h2>".get_the_title()."</h2></li>";
             echo "<li><h2>".the_content()."</h2></li>";
        }
        echo "</ul>";
    }
    wp_reset_query();

    echo "</li>";
     }
    }
   echo "</ul>";
 ?>

更新:管理得更进一步,几乎在那里我想。我现在只能看到一个子帖子(最新的),并且在每个父标题下都会复制相同的子帖子,尽管它不是其他子标题的子项。

任何人都可以帮我指一下最后一点。感谢。

    <?php $parent_pages = get_pages( array(
            'parent' => 0,
            'post_type'=> 'archive'
            ));

        foreach( $parent_pages as $parent_pages)
            { ?>

        <h1><?php echo $parent_pages->post_title; ?></h1>

        <?php
        $children = get_pages(array(
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'post_parent' => $post->ID,
            'post_type' => get_post_type( $post->ID )
            ));

        foreach($children as $child);
        ?>
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>

        <?php } ?>

更新:尝试@robbintt建议,我现在在这里。不确定我是否正确使用了get_page_children并且我现在得到“解析错误:语法错误,意外的T_AS,期待';' “

    <?php
$parent_pages = get_pages( array(
'parent' => 0,
'post_type'=> 'archive'
) );
for ( $parent_pages as $parent_page { ?>
<h1><?php echo $parent_page->post_title; ?></h1>


<?php
$all_pages = get_pages()
$child_pages = get_page_children($parent_pages->ID, $all_pages );
for ( $child_pages as $child_page ) { ?>

    <h2><?php echo $child_page->post_title; ?></h2>
     <p><?php echo $child_page->post_content; ?></p>

<?php } ?>

更新:感谢@robbintt的帮助,这是我的最终工作代码。

     <?php

   $parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive' ) );

   foreach ( $parent_pages as $parent_page ) {
   echo '<h1>';
   echo $parent_page->post_title;
   echo '</h1>';

   $all_pages = get_pages(array( 'post_type'=> 'archive' ) );
   $child_pages = get_page_children($parent_page->ID, $all_pages );
   foreach ( $child_pages as $child_page ) {

   echo '<h2>';
   echo $child_page->post_title;
   echo '</h2>';

   echo '<p>';
   echo $child_page->post_content;
   echo '</p>';
  }
 }
?> 

1 个答案:

答案 0 :(得分:0)

您使用标准while ( have_posts() ) {}

在这里滥用循环

相反,让我们定位父页面,然后使用for循环来获取子页面。

/* here we include only pages with no parent */
$parent_pages = get_pages( 'parent' => 0 ) 

foreach ( $parent_pages as $parent_page ) {

  /* Proceed as you have inside the while loop, targeting $parent_page each time */

}

以下是其他文档,您可以按照自己的意愿进行排序:http://codex.wordpress.org/Function_Reference/get_pages#Parameters


第2部分:

以下是下一个请求的部分,如何获取子页面的信息:

http://codex.wordpress.org/Function_Reference/get_page_children

此函数将返回一个页面子数组。这里的关键是这个循环在另一个循环中运行,实际上获取所有父页面。

$all_pages = get_pages( array( 'post_type'=> 'archive' ) )
$child_pages = get_page_children($parent_page->ID, $all_pages );
  foreach ( $child_pages as $child_page ) {
    /* proceed with any calls on child page such as ID/title, in the format $child_page->ID */

让我知道它是怎么回事!这是我们正在使用的新功能: http://codex.wordpress.org/Function_Reference/get_page_children


第三部分:

这里我已经为子页面部分清理了一些代码。有很多事情要发生,所以我标准化并简化了你如何回应HTML并增加了很多行。这使得人眼可以更清楚地看到设计图案。

<?php

$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive' ) );

foreach ( $parent_pages as $parent_page ) {
  echo '<h1>';
  echo $parent_page->post_title;
  echo '</h1>';

  $all_pages = get_pages( array( 'post_type'=> 'archive' ) );
  $child_pages = get_page_children($parent_page->ID, $all_pages );
  foreach ( $child_pages as $child_page ) {

    echo '<h2>';
    echo $child_page->post_title;
    echo '</h2>';

    echo '<p>';
    echo $child_page->post_content;
    echo '</p>';
  }
}
?>

以下是我对此代码的原始撰写:http://codepad.org/pLtFCI1l