为什么我在4个循环中有相同的内容?(Why I have the same content in 4 loops? WP)

我的主导航菜单下有4个div,我不会在每个div中显示单个类别的最近帖子。 我使用此代码,但在每个div中我都有相同的内容(我更改了类别ID)。

<?php // display a list of recent case studies // instantiate an instance of WP_Query as a variable $recentPosts $recentPosts = new WP_Query(); // get last 10 posts from category 7 (case studies) $recentPosts->query('showposts=4&amp;cat=4'); // utilize two methods to get at template tags (below) while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <div class="slide" ><a href="<?php // bump out what we need courtesy of template tags the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h2><?php the_title(); ?></h2><?php echo excerpt(20); ?></a></div> <?php endwhile; ?>

I have 4 divs under my main navi menu, I wont to display in every single div recent posts from single category. I use this code, but in every div I have the same content (I changed the category-ID).

<?php // display a list of recent case studies // instantiate an instance of WP_Query as a variable $recentPosts $recentPosts = new WP_Query(); // get last 10 posts from category 7 (case studies) $recentPosts->query('showposts=4&amp;cat=4'); // utilize two methods to get at template tags (below) while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <div class="slide" ><a href="<?php // bump out what we need courtesy of template tags the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h2><?php the_title(); ?></h2><?php echo excerpt(20); ?></a></div> <?php endwhile; ?>

最满意答案

试试这段代码:

<?php $args = array( 'numberposts' => 6, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => 3 ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>

Try this code snippet:

<?php $args = array( 'numberposts' => 6, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => 3 ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>为什么我在4个循环中有相同的内容?(Why I have the same content in 4 loops? WP)

我的主导航菜单下有4个div,我不会在每个div中显示单个类别的最近帖子。 我使用此代码,但在每个div中我都有相同的内容(我更改了类别ID)。

<?php // display a list of recent case studies // instantiate an instance of WP_Query as a variable $recentPosts $recentPosts = new WP_Query(); // get last 10 posts from category 7 (case studies) $recentPosts->query('showposts=4&amp;cat=4'); // utilize two methods to get at template tags (below) while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <div class="slide" ><a href="<?php // bump out what we need courtesy of template tags the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h2><?php the_title(); ?></h2><?php echo excerpt(20); ?></a></div> <?php endwhile; ?>

I have 4 divs under my main navi menu, I wont to display in every single div recent posts from single category. I use this code, but in every div I have the same content (I changed the category-ID).

<?php // display a list of recent case studies // instantiate an instance of WP_Query as a variable $recentPosts $recentPosts = new WP_Query(); // get last 10 posts from category 7 (case studies) $recentPosts->query('showposts=4&amp;cat=4'); // utilize two methods to get at template tags (below) while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <div class="slide" ><a href="<?php // bump out what we need courtesy of template tags the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><h2><?php the_title(); ?></h2><?php echo excerpt(20); ?></a></div> <?php endwhile; ?>

最满意答案

试试这段代码:

<?php $args = array( 'numberposts' => 6, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => 3 ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>

Try this code snippet:

<?php $args = array( 'numberposts' => 6, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => 3 ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>