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.
The post ends here. Wanna leave a response? Have Your Say!
Next post: WordPress Posting Issue Using “Div”-Tags
Previous post: Online Color Schemer By Adobe
The above post was initially scribbled on June 8th, 2007 @ 20:15:48 +0200 (CEST) and has been last modified on September 16th, 2008. It is filed under the category WordPress Stuff and has been tagged with the terms [theme | webdesign | wordpress].
Sorry, still gathering information...
Trackback URI | Comments RSS | Permalink



