WordPress.org

Ready to get started?Download WordPress

Forums

Random Posts from Single Category in Sidebar (6 posts)

  1. unknown
    Member
    Posted 7 years ago #

    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

  2. alakhnor
    Member
    Posted 7 years ago #

    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;
  3. unknown
    Member
    Posted 7 years ago #

    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

  4. unknown
    Member
    Posted 7 years ago #

    Hey, where'd you go?

  5. alakhnor
    Member
    Posted 7 years ago #

    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

  6. spotme10
    Member
    Posted 6 years ago #

    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>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.