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.“-
Comment by Guido on 5 August 2010:
Thanx for this. I was looking how to get random posts out of the “WP_Query array” for a custom post type. And by looking at your code i found out its just as easy as “‘orderby’ => ‘rand’”!
Comment by Bedding Collections ` on 12 October 2010:
finance is a good subject, everyone is interested when we talk about money**~
Comment by Canon Camera Bag : on 23 October 2010:
finance is the topic that i always like coz i love to invest and earn money-*-
Comment by Health and Welness Forum · on 13 November 2010:
our bedding collections consists of beddings that come from new york and this are great bedding collections ;’~
Comment by Cross Country Running Shoes on 13 December 2010:
finance is really critical when it comes to the future of businesses and trade. i also took finance during my college days `”*
Comment by Mike on 31 December 2010:
Good Article! Another way to get custom sidebars for your pages or posts is to download and install the Graceful Sidebar Plugin. This plugin creates two additional edit fields in your post edit screen which let you create a custom sidebar widget title and content for each page or post.
Enjoy!
Mike
Comment by Clare on 1 January 2011:
This is just the info I was looking for – although I want to actually add a number of text links to my sidebar – but I think I should be able to take your step and modify it. I’m trying to code a theme and avoid heavy plugins if possible.
Comment by ways to get your ex back on 12 March 2011:
[...] Show random Custom Fields in Sidebar using WordPress | SaijoGeorge.com [...]
Comment by birkenstock clog on 27 March 2011:
Hello, i believe that i noticed you visited my weblog so i got here to ¨go back the want〃.I’m trying to find issues to improve my website!I guess its adequate to use some of your ideas!!
Comment by Bali Hotels Accomodation on 28 September 2011:
Nice post ,thanks for the info. . .^_^