Wordpress Random Post List without plugin
Last Updated on Wednesday, 22 July 2009 06:51 Written by Ray Sunday, 19 July 2009 04:51
Most 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.
- Open your sidebar.php file
- 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>
- 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
Tags: PHP, Random Post, Tutorial, Wordress
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?
Can you help me?
[...] 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 [...]
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