• I have tried several “random post” plugins and can’t seem to get any to do what I’d like.

    In my sidebar, I want to display a list of maybe 5 “random posts” pulled from a single category on my blog. So that the sidebar menu would read:

    Random Posts
    *random post #1
    *random post #2
    *random post #3
    *random post #4
    *random post #5

    All I want to be called in that list is the “Title” of the post, nothing else. The category ID number is 7.

    Can someone please help me with this? I’m not sure if I need to add a plugin or if a simple line of code added to my sidebar.php file would suffice.

    Thanks,
    kopper
    GaragePunk.com

Viewing 5 replies - 1 through 5 (of 5 total)
  • function random_post ($limit)
    {
    	global $wpdb, $wp_version;
    
    	if ($wp_version < '2.1')
    		$post_type_sql = "AND post_status = 'publish'";
    	else
    		$post_type_sql = "AND post_status = 'publish' AND post_type = 'post'";
    
    	$order_by_sql = "rand()";
    
    	// query records that contain img tags, ordered randomly
    	// do not select images from password protected posts
    	$sql = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_content,
    			$wpdb->posts.post_date, $wpdb->posts.post_author
    		FROM $wpdb->posts
    		WHERE post_password = ''
    		$post_type_sql
    		ORDER BY $order_by_sql
    		LIMIT $limit";
    	return $wpdb->get_results($sql);
    }

    That’d be to use instead of a query_post or get_post.

    $posts = randon_post(5);
    foreach ($posts as $post) :
          echo $post->$post_title;
    endforeach;
    Thread Starter unknown

    (@kopper)

    Hey, first, thanks a lot for writing this code! But can you please give me a bit of instruction on where to put it? For example, the top part… does it get saved as a plugin or entered directly into the sidebar.php file as-is? And if so, what do I do with the bottom part?

    Also, I don’t understand the difference between “query_post” or “get_post”… ???

    Thanks again,
    kopper

    Thread Starter unknown

    (@kopper)

    Hey, where’d you go?

    Oops! sorry.
    You can put the function anywhere in your template files and the other code where you want to display.
    However, I didn’t get the Category thing…I’d have to join it with link2post in the sql query. :S

    Hi, wondering how I would incorporate your script with the script I am using to create a list of posts with a thumbnail image.

    <ul id="candidates">
    <?php
    $postslist = get_posts('category=1&order=ASC&orderby=post_title');
    
    foreach ($postslist as $post) :
        setup_postdata($post);
    
    // check for thumbnail
    $thumb = get_post_meta($post->ID, 'thumbnail', $single = true);
    // check for thumbnail class
    $thumb_class = get_post_meta($post->ID, 'thumbnail-class', $single = true);
    // check for thumbnail alt text
    $thumb_alt = get_post_meta($post->ID, 'thumbnail-alt', $single = true);
        ?>
    <li>
    <?php // if there's a thumbnail
    if($thumb !== '') { ?>
    
    	<img src="<?php echo $thumb; ?>"
    	class="<?php if($thumb_class !== '') { echo $thumb_class; } else { echo "left"; } ?>"
    	alt="<?php if($thumb_alt !== '') { echo $thumb_alt; } else { echo the_title(); } ?>"
    	/>
    
    <?php } // end if statement
    
    // if there's not a thumbnail
    else { echo ''; } ?> 
    
    <h3><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?></li>
    
    <?php endforeach; ?>
    </ul>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Random Posts from Single Category in Sidebar’ is closed to new replies.