如何使single.php中的第二个循环工作?
-简单博客 -二十一儿童主题 我需要:single.php中的第二个循环,显示所选帖子和下面的所有其他帖子 到目前为止,我在single.php中的内容(结果是一个空白页):如何使single.php中的第二个循环工作?,php,wordpress,syntax,wordpress-theming,Php,Wordpress,Syntax,Wordpress Theming,-简单博客 -二十一儿童主题 我需要:single.php中的第二个循环,显示所选帖子和下面的所有其他帖子 到目前为止,我在single.php中的内容(结果是一个空白页): 这应该可以做到: <?php get_header(); ?> <div id="primary" class="site-content"> <div id="content" role="main"> <?php while (
这应该可以做到:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
注:
它经过测试并正常工作。因为OP需要所有的帖子,所以
posts\u per\u page
参数应该是-1
。是的,这是有意义的。我在自己的安装中进行测试,如果我不停止循环,它将加载数百篇文章。谢谢。
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->