Show random Custom Fields in Sidebar using Wordpress
Most of us will be using Custom Fields in wordpress these days to add a thumbnail to each of out post. Most of the time this image will be used alongside the post on the homepage and search results. I wanted to show random posts from specific category in the sidebar of my site and instead of using the traditional way of linking the posts via text link, I wanted to use the images I used for the Custom Fields to link to the post.
I wanted to get the posts from a specific category in this case from the category named Games in the sidebar. So first we need to add a plugin that will allow us to run php by adding it through the widget. You can skip this step if you are going to edit sidebar.php and add the required code directly in to the file. But I choose to go with the widget option.
First Download and Install Samsarin PHP Widget. One it is installed drag and drop Samsarin PHP Widget in to your sidebar. Then add the following code to the widget.
<?php
$random_query = new WP_Query(array(
'post__not_in' => $do_not_duplicate,
'showposts' => 6,
'cat' => 10,
'orderby' => 'rand'
));
while ($random_query->have_posts()) : $random_query->the_post(); ?>
<div class="featured-random-apps"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php $values = get_post_custom_values("Thumbnail"); echo $values[0]; ?>" alt="alt-text" width="85" height="85" border="0" /></a></div>
<?php endwhile; ?>
These are the things you can change
’showposts’ => 6, ( change 6 to how many post you wnat to show )
‘cat’ => 10, ( the category ID for the category that you want to show in the sidebar )
get_post_custom_values(“Thumbnail”); ( the custom Field I want in the sidebar is Thumbnail, chnage it to what ever you want )
Hope this helps some of you guys out there.
See the hack in action on http://www.bestwindowsmobileapps.com. Both Random WinMo Apps and Random WinMo Games in the sidebar use this.







Comment by DanielUP on 19 February 2010:
It was pretty much helpful, thank you!
Nevertheless, I have a question: using the Custom Fields I would like to include some html text… but somehow what appears is the code, not the text with format.
Is it possible to fix this?
Thank you in advance!
Comment by Saijo George on 19 February 2010:
In Wordpress there is always a way
try this plugin : http://www.matteoionescu.com/wordpress/embed-html/
Image : http://www.matteoionescu.com/wordpress/wp-content/uploads/2008/02/immagine-3.png
Comment by Aubrey Roberts on 5 May 2010:
I have 100 domains on Godaddy and i can say that this company is very reputable.***
Comment by Carson Washington on 26 May 2010:
i am into Business and Financing. it is really very interesting how to make money both online and offline.*;*
Comment by Joseph White on 10 July 2010:
anybody knows how i could get my banned Adwords account back?;;:
Comment by Alice Kelly on 19 July 2010:
finance is a great topic that involves a lot of money and also investments.“-