Showposts Loop
-
I’m trying to show the 5 recent posts in my bar with this loop:
<?php query_posts(‘showposts=5’); ?>However, it changes my main index content from showing 3 blog posts to 5 posts.
Is there a way to show recent posts in my sidebar without it interfering with my main content?
Note: I do not want to use the “recent posts” widget because I am using my own alteration of the loop. Example below:
<?php query_posts('showposts=5'); ?> <ul> <?php while (have_posts()) : the_post(); ?> <li class="latestpost"> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <br /><span class="latestpostmeta"><?php the_time('M jS, Y') ?> | <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span></li> <?php endwhile; ?> </ul>
You can view it at http://kisschanel.com/blog, located under “Recent Posts” in the sidebar.
-
Try the get_posts function, rather than query_posts:
Thanks for the response ambrosite. But I tried that already and it only fetched 3 posts, which is what’s set in my Reading settings. I need for it to be different than what’s showing on the main content.
In that case, you’ll need to create a custom loop using WP_Query. Something like this:
<?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=5'); while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> <p><?php the_content(); ?></p> <?php endwhile; ?>
That didn’t work ambrosite. Here’s the scenario:
left side : Only supposed to show 3 blog posts in it’s entirety.
right sidebar : Only supposed to show 5 listings of the recently posted blog TITLES, date, and comment count. Not the content.OK, well if you want the date to display instead of the content, just replace the_content with the_time, like this:
<?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=5'); while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> <p><?php the_time(); ?></p> <?php endwhile; ?>
If you paste that code into your sidebar, does it display the five posts you are looking for?
Actually, building on the code snippet you posted originally, I suppose what you really want is this:
<?php $recentPosts = new WP_Query(); $recentPosts->query('showposts=5'); ?> <ul> <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <li class="latestpost"> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <br /><span class="latestpostmeta"><?php the_time('M jS, Y') ?> | <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span></li> <?php endwhile; ?> </ul>
That last submission worked perfectly. Thank you ambrosite!
- The topic ‘Showposts Loop’ is closed to new replies.