Tuesday, February 09, 2010

Site Search

Wordpress Random Post List without plugin

avatarMost bloggers out there should have known the purpose to list a random post. That is so that your old post can still showed up and indexed by Search Engine especially Google. There are a lot of wordpress plugin who can do this. But too much plugin will cause wordpress to slow down.

This tutorial will show you how to make a random post list on your sidebar. Just like before it also based on anime-zone.net’s random post when viewing their post. This will be useful for a theme developer so they can include a random post capability on their theme.

  1. Open your sidebar.php file
  2. Put these codes:
    <ul style="margin-left:5px; margin-right:5px">
    <?php $cat_posts = get_posts('numberposts=5&category=5&orderby=rand'); ?>
    	<?php foreach($cat_posts as $post) { 
    		setup_postdata($post);
    	?>
    		<li class="cat-posts-item-<?php the_ID(); ?>" style="height:55px">
    			<div>
    			<a style="font-weight:bold; color:#1a7000" href="<?php the_permalink() ?>" rel="nofollow"><?php echo custom_trim_title(30); ?></a><br />
    			<?php the_time('j F Y') ?><br />posted by <?php the_author(); ?>
    			</div>
    		</li>
    	<?php } ?> 
    </ul>
  3. Save the file

Surely, the codes might need to be changed a bit but keep this line:

<?php $cat_posts = get_posts('numberposts=5&category=5&orderby=rand'); ?>

From that line, we know that it will retrieve random five post from category id 5 which is in this case is for Lyrics category. the key to the random post is this “orderby=rand” parameter.
And to show the list, we use :

<?php foreach($cat_posts as $post) { 
setup_postdata($post);
?>

instead of the normal loop so that it won’t affect the main loop if you were to show the random post on home, archive, or single page



4 Comments

  1. Comments  Andrian   |  Saturday, 08 August 2009 at 8:56 PM

    Please help me, i think at something else, i don’t know to write the php code. I want to ad a section in sidebar “My best posts” (5 or 7) random selected urls from a predefined list of 40-50 url’s manually added by me in a separate php file.
    There must be done two things.
    1. Line of code from the box <?php wp_get……..
    2. php file containing the url list added by me one per line and the little script of course!

    Can you help me?

  2. Comments  Key Ödemeleri   |  Sunday, 30 August 2009 at 8:58 AM

    Can you help me?

  3. Comments  How To Recycle Old Posts, and Why You Should | Life Here And There   |  Wednesday, 02 September 2009 at 11:56 PM

    [...] tagged “myfavs” using some information from the Wordpress Codex but you can also use a hack of your own, or a plugin, but remember to watch the number of plugins you’re [...]

  4. Comments  Hemalatha   |  Wednesday, 23 December 2009 at 7:03 PM

    place your code on my web site but after refresh page view error for Fatal error: Call to undefined function custom_trim_title() what we do.
    please help me

Leave a Reply




Featured Links