Tag WordPress

WordPress: Display Recent, Related or Random Posts in Your Sidebar

You may have noticed that I have dynamic sidebar that changes slightly as you browse the different parts of the site. For example, I have random posts in my home sidebar, while this changes to related posts when browsing a specific post.

One visitor emailed me recently asking me if I could tell her how I managed to display recent, related or random posts in my sidebar.

Well, I neither use a plugin nor SQL statements for this part.

I used the conditional tags to check the current page and to execute the right part of the code.

The Code in Pieces

I divided the code into three pieces, so you can understand it easily.

Recent Posts


<div class="widget">
<h2>Recent Posts</h2>
<ul>
<?php $posts = get_posts('numberposts=3'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
</li>
<?php } ?>
</ul>
</div>

The above code picks the latest three posts. You can change the number of posts displayed by changing number in get_posts('numberposts=3'); to whatever you like.

Related Posts by Category


<div class="widget">
<h2>Related Posts</h2>
<ul>
<?php $posts = get_posts('numberposts=3&category='.$category->cat_ID.'&exclude='.$current_post);
foreach($posts as $post) {
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>"><?php the_title() ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
</li>
<?php } ?>
</ul>
</div>

We used get_posts('numberposts=3&category='.$category->cat_ID.'&exclude='.$current_post); to make all the business easier. $category->cat_ID pulls the current post’s category, exclude='.$current_post makes sure to exclude the current post from the related ones.

You may use related posts by tag alsoWP Recipes

Random Posts


<div class="widget">
<h2>Random Posts</h2>
<ul>
<?php $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?><br />
<span><?php the_excerpt(); ?><br /><em>&mdash;Posted on <?php the_time('n/j/Y') ?></em></span></a>
</li>
<?php } ?>
</ul>
</div>

Our code uses orderby=rand. So WordPress will pick 3 random posts from the database every time our site loads.

Final Thoughts

The above codes can be merged together and displayed on the sidebar using the WordPress conditional tags.

If you have enjoyed this post or have any questions or recommendations, please share your views in the comments section.

WordPress now supports Daylight Saving Time

Daylight Saving Time (DST) or Summer Time (as they call it in Britania) was usually something we used to set by hand in WordPress releases beyond the in-development version 2.8. This was not really painful, but being a great software, WordPress guys described it as a “Lame”.

What is daylight saving time?

Daylight saving time (Summer Time) is the convention of advancing clocks so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn. Modern DST was first proposed in 1895 by George Vernon Hudson, a New Zealand entomologist. Many countries have used it since then; details vary by location and change occasionally.

Wikipedia

In WordPress 2.8, they added Timezone enhancements for PHP 5 so you can choose a city in the same timezone as you for Timezone in Administration > Settings > General.

Old WP time settings

Old WP time settings

New WP DST settings

New WP DST settings

See also

How to make your WordPress posts dates clickable

Do you know that there’s another way to incorporate links to your blog archive in your site without using the archives by date widget by assigning every date on your blog were clickable! Every bit of every date your site contains, and by assigning every bit of your posts date to a link, and by every bit I mean the three major parts the average date format contains —month, day and year.

I know many of you are already aware about this little trick, but I posted this because many people may need it.

The idea

I was recently designing a new blog theme and I came across an old blog: 1976design.com, the post dates were clickable and were driving reasonable traffic to other parts of the blog. The blog has nothing to do with WordPress and its run by hand crafted blog application written by the owner of the website, but it still inspired me to implement the idea on my theme.

Implementation

Open your theme’s index.php and find the template tag <?php the_time(); ?> then replace that with the following code:


// The month
<a title="<?php the_time('F') ?>" href="<?php bloginfo('url'); ?>/<?php the_time('Y') ?>/<?php the_time('m') ?>"><?php the_time('F') ?></a>
// The date
<a title="<?php the_time('F') ?> <?php the_time('jS') ?>" href="<?php bloginfo('url'); ?>/<?php the_time('Y') ?>/<?php the_time('m') ?>/<?php the_time('j') ?>"><?php the_time('jS') ?></a>,
// The year
<a title="<?php the_time('Y') ?>" href="<?php bloginfo('url'); ?>/<?php the_time('Y') ?>"><?php the_time('Y') ?></a>

The above code will show something like this: May 14th, 2009.

If you think there is a better way to do this or if you have something else to add or to say about this, please share it in the comments section.