【问题标题】:I can't isplaying Child pages of the current page in post format (Wordpress)我无法以帖子格式播放当前页面的子页面(Wordpress)
【发布时间】:2011-01-18 16:19:32
【问题描述】:

我从 Wordpress codex 中获取了this code,它以帖子格式显示当前页面的子页面。

我创建了一个名为 "Home" 的页面,并为其分配了一个名为 Front Page 的模板:

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');
   $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(); ?>

然后我将 关于 页面设为子页面(主页的子页面)。

由于某种原因,没有显示任何内容(见模板底部)

有什么建议吗?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我猜你的问题是 $post 被分配在循环内部,而该代码被放置在循环之外。

    问题可能只是 $post->ID 的一个实例;你的页面结构很奇怪。尝试使用此行而不是您发布的类似行:

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

    【讨论】:

    • 你的意思是我应该把它放在这个循环中吗?:get_header(); ?&gt; &lt;?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?&gt; &lt;?php if ( is_front_page() ) { ?&gt; &lt;h2&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt; &lt;?php } else { ?&gt; &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt; &lt;?php } ?&gt; &lt;?php the_content(); ?&gt; &lt;?php wp_link_pages( array( 'before' =&gt; '' . __( 'Pages:', 'twentyten' ), 'after' =&gt; '' ) ); ?&gt; &lt;?php edit_post_link( __( 'Edit', 'twentyten' ), '', '' ); ?&gt; &lt;?php comments_template( '', true ); ?&gt; &lt;?php endwhile; ?&gt;
    • 是的!请试一试。很多 WP 函数只能在 The Loop 内部运行。
    • @aendrew 我应该把它放在循环的哪个部分? (在哪一行之前和之后?)
    • 实际上,尝试将代码留在原处并将 $post->ID 更改为 $wp_query->post->ID,因为您在循环之外。
    • @aendrew 成功了,感谢您今天帮助我!那么当我将 $post->post->ID 更改为 $wp_query->post->ID 时发生了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多