Kamrul Hassan

Blog tips #1 - displaying recent posts, comments and features

 Posted on February 28, 2007 at 11:39 am |  By Kamrul Hassan
 Leave a Comment, 16 Comments so far

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:

sidebarNow 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&nbsp;*” . “<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.

There are currently 16 Comments on this post
 Leave a Comment   Listen to this Listen to it   Print it Print it   Share it

16 Comments So far, Leave a Comment.

RSS Feed for comments TrackBack URI

  • 4Avatars v0.3.1
    Chang Woo
    flag

    March 1, 2007 @ 7:47 pm

    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!

  • March 1, 2007 @ 8:47 pm

    Chang,
    Sure it works in all language. Even if you create a WordPress blog for Japanese or any other language it just change the character set for display, but the core remain same.

    These codes works with WP core, so you don’t have to worry, though character count might be different, as i am not familiar with Japanese character set. I know one Chinese character is equivalent to two UTF-8 char. Do some experiment to get the right character length.

  • March 1, 2007 @ 10:10 pm

    Thanks! Your example for displaying the most recent posts is exactly what I was looking for.

  • March 1, 2007 @ 11:13 pm

    Michael - Kamrul is going to blog all the great things he has done throughout the site. Note that it’s a Web site and not just a blog that resides on wordpress. In fact, Donncha from Wordpress blogged about the site :)

  • 4Avatars v0.3.1
    flag

    April 11, 2007 @ 10:13 am

    Blog Tips#2 Increase readership through subscription

    [...] In my last “Blog tips” post, I discussed how to make it easy for readers to find the content they’re looking for. This time I’m going to talk about how to substantially increase your readership and more importantly, how to keep readers informed or interested, to ensure they continue to come back. [...]

  • 4Avatars v0.3.1
    m0n
    flag

    May 13, 2007 @ 4:28 pm

    Thank you for this valuable information. The last bit there about ”

    myposts=get_posts

    ” is exactly the solution I needed. Cheers!

  • July 17, 2007 @ 9:13 am

    Hi

    I have tried you Displaying Recent Comments code in my sidebar but get the following error:

    Parse error: parse error, unexpected T_STRING in /var/www/html/rol/wp-content/themes/ad-flex-blog4/syndication/syndication_custom_sidebar.php on line 16

    Is there anything I can do to correct this please?

    Regards

  • July 17, 2007 @ 1:23 pm

    John, you are missing a ‘ or ” somewhere on the top portion of the code. The best thing to do is copy the above code in a notepad file and check if any extra character added somewhere. or if you feel comfortable you can just download this code snipet from here
    (http://segala.com/displaying-comments.txt).

  • July 17, 2007 @ 2:17 pm

    Hi Kamrul

    Still no go.

    Line 24 says

    $output .= “\n\t”.strip_tags($comment->comment_author) .” says ” . “ID) . “#comment-” . $comment->comment_ID . “\” title=\”on ” . $comment->post_title . “\”>” . strip_tags($comment->com_excerpt) .”…”;

    Can you spot anything wrong? Thanks

    John

  • July 17, 2007 @ 2:53 pm

    Yes John,

    it should be either this (without the link to the comment):

    "\n\t
  • ".strip_tags($comment->comment_author) ." says " . strip_tags($comment->com_excerpt) ."…
  • ";

    or this(with the links to the comment):

    i know the real crap is just the ascii adding in the end, when you are copying it from this comment.

  • July 17, 2007 @ 5:22 pm

    Hi Kamrul

    Thanks again for all your help with this. The mod code runs just fine.

    Regards

    John

  • July 18, 2007 @ 8:36 am

    Hi John,
    no problem, glad i could help. Thanks.

  • December 2, 2007 @ 6:12 pm

    will it works to show the SEO friendly links instead of the id’s appended at the end?

  • April 1, 2008 @ 5:42 pm

    Oh this is what I needed, thanks! Trying to get people to stay on my site once they come to a particular page. Hopefully this helps keep them on my site reading more!

  • 4Avatars v0.3.1
    M
    flag

    June 10, 2008 @ 7:29 pm

    Hi. Can I use these codes in my theme which I making?

  • June 11, 2008 @ 1:31 pm

    Yes sure you can use it, since WordPress its self is GPL, thus all codes belongs to it, as well falls under GPL licence.

Leave a comment


We're constantly spammed by people who have as much life as the robots they use. So, we hope you don't mind if we moderate your comment if it's your first time on this blog.

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Live Preview of your comment-

 
Close
E-mail It