• Home
  • Contact
  • About Me
  • Home Jobs
  • Maldives
  • Technology
  • Recipes
  • Subscribe

Kishore

Limits Undefined

Creating a Widget for Displaying Random Category Post

by Kishore on March 15, 2009

  • Tweet
I just created a small hack to display posts from different category randomly. The advantage is of this widget is that you can show some posts to your visitors which could be hidden in your blog. I have been using this widget for a few days now, so I am not in a position to claim any pros of this widget. I assume that this could help in reducing the bounce rate and the time spent on the site to increase a bit.

I have tested this plugin – I did not find any problem with page loading or any other side effects yet. You feedback could help me in improving this hack. I will release this as a plugin if it works well for me. I am posting the code below so you can use it and let me know your comments

<?php $category_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = 'category'"); ?>
    <?php $r = rand(1,2); ?>
    <?php $rcat = rand(1, $category_count); ?>
    <?php $kish_cat = get_categories();  ?>
    <?php if ($r==1) { ?>
    <?php $catname = $kish_cat[$rcat]->cat_name; ?>
    <?php $catid = $kish_cat[$rcat]->cat_ID; ?>
    <?php //echo "Catogory name is ".$catname."random category is ".$category_count."-".$rcat; ?>
    <div class="video">
    <?php print "<h3>Some Posts From $catname</h3>"; ?>
        <?php $totalen = 85; ?>
                <?php $myquery = "showposts=8&cat=".$catid; ?>
                <?php //print $myquery; ?>
                <?php $recent = new WP_Query($myquery); while($recent->have_posts()) : $recent->the_post();?>
                <?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
                <p style="padding:2px; font-size:11px;">
                    <a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left;margin:2px 10px 0px 0px;width:30px;height:30px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
                <?php else: ?>
                <p style="font-size:11px;">
                       <a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left;margin:2px 10px 0px 0px;width:30px;height:30px;"  src="<?php echo $sThumbUrl; ?>" alt="<?php the_title(); ?>" /></a>
                <?php endif; ?>
                <?php $tit = get_the_title(); ?>
                <?php $titlelen = strlen($tit); ?>
                <?php $contentlen=$totalen-$titlelen; ?>
                <?php $cont = the_content_footer($contentlen); ?>
                <a href="<?php the_permalink() ?>"><?php echo $tit; ?></a> - <?php echo $cont." ..</p>"; ?>
                <?php //echo $titcont." ..."; ?>
                <div style="margin-bottom:1px; padding:0px 0px 2px 0px; clear:both;"></div>
                <?php endwhile; ?>
    </div>

You can post this code to your blog sidebar. Please change the div class names to your blog name

One more which displays the thumbnails for the latest 20 posts (You can change this number according to your need).

<div class="video"">
    <h3>Latest Posts From All Sections</h3>
                <?php $recent = new WP_Query("&showposts=20"); while($recent->have_posts()) : $recent->the_post();?>
                <?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
                <span><a class="linkopacity" href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" style = "float:left; margin:0px 10px 10px 0px; width:50px; height:50px;" title = "<?php the_title(); ?>" alt="<?php the_title(); ?>" /></a></span>                  
                <?php else: ?>
                       <span><a class="linkopacity" href="<?php the_permalink() ?>" rel="bookmark"><img style = "float:left; margin:0px 10px 10px 0px; width:50px; height:50px;" src="<?php echo $sThumbUrl; ?>" alt="<?php the_title(); ?>" /></a></span>
                <?php endif; ?>
                <?php endwhile; ?>
    </div>

The left one is for the random category and the right one is for the Latest 20 posts. I use an hack for automatically updating the thumbnail custom field.

Tagged as: "Thumbnail", <img src=", Catagori php, Display by random one post from multiple blogs in wordpress, Font size, Get_post_meta in php widget, Get_post_meta php, Hacks, Php get_post_meta, Random category wordpress, Themes, Thumbnails, true);?>" />, Widget catagori on php, Widget for post category, Wordpress

  1. Delete WordPress Post From Frontend : If you are logged in as...
  2. Custom Fields in Windows Live Writer : I have been using themes which...
  3. Display UpComing Posts on your Sidebar – WordPress Blogs : I am sure most of the...
  4. Gigya – Widget for Bookmarking, Emailing, Posting : Today I have added the Gigya...
  5. Adding Text Links to below Post for RSS Feeds : Adding sponsored links which is added...

Recently Updated

  • Next iPad could have LED back light screens
  • Salute to Sachin Tendulkar on his 50th Century
  • Neer Dosa
  • 5 Reasons Why You Should Respond to Every Comment
  • Dhiraagu Celebration 20 years of services
  • Chamundi Hills – Mysore
  • New Color Coded Category Theme
  • Google Trust
  • Presidential Suite at a resort in Maldives
  • Importance of a Good Accommodation

iPhone / iPad / Apple News

  • The new iPad already Jailbroken
  • Kindle for iPad updated for Retina Display – New iPad Features
  • First new iPad buyer – David Tarasenko from Australia
  • The New iPad (3rd Generation) Reviews
  • Better Battery on the new iPad compared to iPad 2
  • No it is not iPad 3 or iPad HD, it has no name
  • New iPad with Bluetooth 4.0 – What does that mean to a common user?
  • iPad 3 Shipping has started
  • Task Management App for iPad – Beesy – iPad To-Do
  • What is in iOS 5.1 Update ?
← Fire , 15 Under Restaurants at W Resort Maldives
Tweaking WordPress Blog For Search Engines →
Hey!! - Like us on FaceBook

Get our posts via email - Yes it's free:

Devu and Suraj are the Munch Dance Dance Winners

Devu and Suraj are the Munch Dance Dance WinnersTweet Munch Dance Dance is one of the dance reality show aired in Asianet channel on Saturdays and S...

Samsung Galaxy Note Android 4 leaked – AT&T

Samsung Galaxy Note Android 4 leaked – AT&TTweet Samsung Galaxy Note Rom is available for download, but this is only for AT&T users (This ...

Why did I switch over to Samsung Galaxy Note from iPhone 4?

Why did I switch over to Samsung Galaxy Note from iPhone 4?Tweet I am currently using a Samsung Galaxy Note and I was using an iPhone 4 and an iPad 2 along wit...

IPL 5 – 2012 Matches Schedule

Archives | Advertise | Contact | Disclaimer | Write For Us
Maintained By Kishore