Relying on plugins is a little unreliable so I’ll show you how to achieve the same functionality through tweaking some code. Don’t be afraid if you’re not used to tweaking code as I’ll make easy. I’ll also provide you with some support if you drop by with questions
Displaying recent posts
Most bloggers use third party plugins to show recent posts in the sidebar. Although this is the easiest way around, it certainly isn’t the most effective and customizable means.
Prior to WordPress 2.1 the most popular plugins for this purpose was “Customizable Post Listings“. These no longer work in WP 2.1 due to several reasons, of which some of them include WP function differences and extensive use of globals $table.
A lot of other plugins that are used to fetch recent posts lists are also incompatible with last few WordPress updates. Compatibility problems don’t occur when the plugin authors regularly update their plugins, but this doesn’t always happen. So, in our opinion, it’s wise not to be dependent on plugins when you can make a few tweaks to WP functions yourself to achieve the same results.
Fetching recent posts list using wp_get_archives()
This is a WordPress built-in function, a small piece of code that can bring you so much without have to use a single plugin. This function retrieve post archive related information.
The function: <?php wp_get_archives(’arguments‘); ?>
The arguments:
- type- monthly, daily, weekly, postbypost (string type)
- limit- Number of post to fetch, if not use function will show all. (integer type)
- format- Define list format, available format is html(default),option,link,custom.(string type)
- before- In html for format option text to place before the list,there is no default. (string type)
- after- In html for format option text to place after the list,there is no default. (string type)
- show_post_count- Define number of posts in an archive (1 - true) or do not (0 - false). Use only when type is set to ‘monthly’. Defaults is 0.
The example:
Now that you know what the structure of the function and arguments is, let’s put it into action. In our RHS (right sidebar of blog) you’ll notice we have a list of 10 most recent posts. So you just add the following code in your sidebar.php(left or right if you have two sidebar) before the termination(</div>). The screenshot shows where to place the code using theme editor, double click to see larger image.. Here’s the code for that-
<ul> <?php wp_get_archives(’type=postbypost&limit=10‘); ?> </ul>
The orange coloured ‘10′, represents the number of latest post we are displaying. The postbypost is the type, which bring reverse chronological posts list, we chose for our list. You can change these two values to fit your needs, as well as use other arguments to customize the functionality. Wrap it up with CSS to sync with your site navigation.
Now paste that one line code into sidebar(sidebar.php), or wherever you want to show the recent post list. Remember, whether you are placing the code in sidebar.php or category template file (usually category.php) keep the code between list declaration(ul).
Displaying recent comments
Displaying the most recent comments made throughout your blog is a way of highlighting who said what. This in our opinion, encourages some readers to check out what specific commentators are saying. This will hopefully encourage them to comment.
Unfortunately WordPress doesn’t have a one line function for showing recent comments like recent posts. So if you want to show recent comments, you either have to get a plugin (ex-Recent Comments 2.0 works with WP 2.1) or do it our way, which provides you with more flexibility.
Our way is very straight forward, just paste the code below into your sidebar(left or right sidebar.php), or wherever you want to show the recent comments.
Customization tips are bellow the code.
<?php
global $wpdb;$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = ‘1′ AND comment_type = ” AND
post_password = ”
ORDER BY comment_date_gmt DESC
LIMIT 10“;
$comments = $wpdb->get_results($sql);$output = $pre_HTML;
$output .= “\n<ul>”;
foreach ($comments as $comment) {$output .= “\n\t<li>”.strip_tags($comment->comment_author)
.”*says *” . “<a href=\”" . get_permalink($comment->ID) .
“#comment-” . $comment->comment_ID . “\” title=\”on ” .
$comment->post_title . “\”>” . strip_tags($comment->com_excerpt)
.”</a>…</li>”;}
$output .= “\n</ul>”;
$output .= $post_HTML;echo $output;
?>
The red coloured ‘30′ represents how many characters of comments will be shown in the list, and the orange coloured ‘10′ represents how many comments it will fetch. This code by default ignores the password protected post’s comments as well as unapproved comments. This will display “comment author name” says “partial comment linked to the comment”.
The cool part of this code is you can combine it with other codes and functions, just like we did to show the flags (we will have a article on this soon as it’s quite lengthy).
Displaying summary of posts and more
It’s possible to highlight specific posts that are no longer visible on the page, or in recent posts. You may also wish to show a teaser for a few important posts. The method is very simple, it uses a built in WP function, so no need for a plugin.
Just copy the code below and paste it in your desired location [sidebar.php before </div>], the customization tips can be found bellow the code.
<ul>
<?php
$myposts = get_posts(’numberposts=5&offset=1‘);
foreach($myposts as $post) :
?>
<li><a href=”<?php the_permalink(); ?>”><?php the_title();
?></a> …. <?php the_excerpt(); ?></li>
<?php endforeach; ?>
</ul>
The orange coloured ‘5′ represents number of post’s summary you want to show. The red coloured ‘1′ represents offset from the last post, meaning, how many post behind the last entry of your blog. For example, if your blog is set to display 5 post at the front page, “offset=5″ will fetch you the 6th post which isn’t in the front page.
You can do a lot more with this piece of code. You can display specific posts by assigning them to a category (e.g. featured), just add *category=category ID number *at the get_posts arguments.[e.g. get_posts('numberposts=5&category=1')]. This is useful if you’ve written a post, it’s gone out of sight and you now want to highlight it again. Simply edit the post and add it to the ‘featured’ category.
You can sort the list by ascending (ASC) or descending (DSC) by adding the argument order=ASC & orderby= any wp_post table field name. Whatever you do, keep in mind that you have to use it within <ul> & </ul>tags, otherwise you will get only the first post in the list.



Posted on February 28, 2007 at 11:39 am |
By

16 Comments
So far,

March 1, 2007 @
Chang Woo
Cool tips!
But will this work even in other language? Especially the the_excerpt() part.
I am supposed to start a Japanese blog for my study project; it will be great help if you discuss internationalization dependency.
Advance Thanks!