开发者

I can't isplaying Child pages of the current page in post format (Wordpress)

开发者 https://www.devze.com 2023-02-05 10:01 出处:网络
I took this code from the Wordpress codex which displays Child pages of the current page in post format.

I took this code from the Wordpress codex which displays Child pages of the current page in post format.

I created a Page called "Home" and assigned a template called Front Page to it:

front-page.php:

<?php
/**
 * Template Name: Front Page
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
 get_header(); ?>
 <div id="intro">
  <div id="tagline">
   <?php // Retrieve a list of latest posts or post(s) matching criteria).
   $args = array('category_name' => 'Tagline', 'numberposts' => 1, 'order' => 'DESC');
  开发者_JAVA百科 $customposts = get_posts($args);
   foreach($customposts as $post) : setup_postdata($post); ?>
    <h2 class="entry-title"><?php the_title(); ?></h2>
    <div class="entry-content">
     <?php the_content(); ?>
    </div>
   <?php endforeach; ?>
  </div><!-- #tagline -->
  <div id="featured">
   <img src="<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'intro_image'); ?>" />
  </div>
 </div><!-- #intro -->
 <div id="main-content">
  <div id="main-content-first">
   <?php // Retrieve a list of latest posts or post(s) matching criteria).
   $args = array('category_name' => 'Main Content First');
   $customposts = get_posts($args);
   foreach($customposts as $post) : setup_postdata($post); ?>
    <h2 class="entry-title"><?php the_title(); ?></h2>
    <div class="entry-content">
     <?php the_content(); ?>
    </div>
   <?php endforeach; ?>
  </div>
  <div id="main-content-middle">
   <?php // Retrieve a list of latest posts or post(s) matching criteria).
   $args = array('category_name' => 'Main Content Middle');
   $customposts = get_posts($args);
   foreach($customposts as $post) : setup_postdata($post); ?>
    <h2 class="entry-title"><?php the_title(); ?></h2>
    <div class="entry-content">
     <?php the_content(); ?>
    </div>
   <?php endforeach; ?>
  </div>
  <div id="main-content-last">
   <?php // Retrieve a list of latest posts or post(s) matching criteria).
   $args = array('category_name' => 'Main Content Last');
   $customposts = get_posts($args);
   foreach($customposts as $post) : setup_postdata($post); ?>
    <h2 class="entry-title"><?php the_title(); ?></h2>
    <div class="entry-content">
     <?php the_content(); ?>
    </div>
   <?php endforeach; ?>
  </div>
 </div><!-- #main-content -->

<?php
 $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
 $count = 0;
 foreach($mypages as $page)
 {
  $content = $page->post_content;
  if(!$content)
   continue;
  if($count >= 2)
   break;
  $count++;
  $content = apply_filters('the_content', $content);
 ?>
  <h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
  <div class="entry"><?php echo $content ?></div>
 <?php
 }
?>


 <?php get_footer(); ?>

Then I made the About page its child (Home page's child).

For some reason nothing is being displayed (see at the bottom of the template)

Any suggestions?


I'm guessing your issue is that $post is assigned inside of the loop, and that code is placed outside of the loop.

The problem might just be the one instance of $post->ID; your page's structure is pretty weird. Try using this line instead of the similar one you posted:

$mypages = get_pages('child_of='.$wp_query->post->ID.'&sort_column=post_date&sort_order=desc');

0

精彩评论

暂无评论...
验证码 换一张
取 消