Marco Luthe Online! » WordPress Stuff » Display Recent Posts And Comments
Display Recent Posts And Comments
For my own theme – which I am currently designing – I wanted the 5 most recent posts to be displayed in the right sidebar. So I tried to use the query_posts() template tag with the parameter ‘showposts=5′.
Well, it worked for the sidebar – the only problem was: now each and every post was shown in the main section of the page, even if the URL pointed to just one single post! I tried to set back the parameters using $query_string, but that did not work.
That made me kind of wonder why it works in my current (alternated) MistyLook theme: I use the same method to display the 5 most recent posts in the sidebar – and it works fine!
I found out that – in the MistyLook theme – the sidebar is included AFTER The Loop, which means that the main section remains unaffected by calling query_posts(). In my own theme, the right sidebar is included BEFORE, and that means that it will also affect The Loop, i.e. the original query is lost – WordPress “forgets” that e.g. a single post is supposed to appear.
I did a Google search and happened to find the get_posts() template tag which does not change the SQL query and works fine.
I changed the code as follows:
<ul class="sidebox_ul">
<?php
global $post;
$recent_posts = get_posts('numberposts=5');
foreach($recent_posts as $post) : ?>
<li class="sidebox_li"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
This works great!
I also looked for a similar tag for comments, but found none. So I think I will remain using the WP Plugin Recent Comments.
Filed under: WordPress Stuff · Tags: theme, webdesign, wordpress











