我从Wordpress codex中取出this code,以后期格式显示当前页面的子页面。
我创建了一个名为“Home”的页面,并为其分配了一个名为 Front Page 的模板:
前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');
$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(); ?>
然后我将关于页面作为其子页面(主页的孩子)。
由于某种原因,没有显示任何内容(请参阅模板底部)
有什么建议吗?
答案 0 :(得分:1)
我猜你的问题是$ post是在循环内部分配的,而且代码放在循环之外。
问题可能只是$ post-&gt; ID的一个实例;你的页面结构非常奇怪。尝试使用此行而不是您发布的类似行:
$mypages = get_pages('child_of='.$wp_query->post->ID.'&sort_column=post_date&sort_order=desc');