Support » Fixing WordPress » [Plugin: Yet Another Related Posts Plugin] Showing more than one random post

  • First of all my compliments on this wonderful plugin. Works very well.

    I would like to change the number of random posts that are shown when there are no related posts to more than one.

    Surprisingly, changing the limit in


    from 1 to a larger number has no effect. In fact, to my surprise, even totally deleting the said two lines had no impact. It still showed one random post.

    Any help will be appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • @investor – my guess is that this is some interaction with a caching plugin. Completely removing it not affecting anything is very bizarre. Try turning off any caching you might have first.

    Hi Mitch,

    Thanks for the prompt response. Greatly appreciated.

    Is it possible the command to show 1 random post is coming from some other file and not the template file?

    To overrule the caching possibility, I did a fresh install of WP and YARPP on my local machine, ensured there were sufficient number of similar and dissimilar posts, changed the match threshold, ensured the cache was rebuilt after every change, even changed browsers – but still no change.

    Quite perplexed.

    I changed the else command:

    <?php else: ?>
    <br />
     <p>No related posts were found. The Latest five posts are as follows:</p>
     <br /><br />
        $recentPosts = new WP_Query();
        $recentPosts->query('showposts=5'); ?>
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>

    This works in the sense that more one post is shown though the same post is repeated throughout.

    Even the <?php rewind_posts(); ?> command had no effect.

    Is there an alternate else command you can write up?

    @investor – hmm, this does seem quite odd. I’m sorry I personally haven’t played around with many other else functionality before. :/

    Dear Mitch,

    First of all, many thanks for selflessly, tirelessly and cheerfully serving the community. You are an inspiration for us folks. Such good work has its own rewards.

    Coming to my query, for some reason I can’t get more than one random post to show using the random-template (do others have the same problem?).

    This is the code in the random template:

    <?php else:
    <p>No related posts were found, so here's a consolation prize: <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>.</p>
    <?php endif; ?>

    Is it not lacking the ‘loop’ to show more than one post….?

    Using other loop methods shows multiple posts though the same posts are repeated.

    Anyway after much ‘copy-paste’, I stumbled on a solution using the code found here:

    Just add the following after the <?php else: ?> and before the <?php endif; ?> command in the example-template.

    <br />
     <p>No related posts were found. The Latest five posts are as follows:</p>
     <br /><br />
    //db parameters
    $db_username = '#';
    $db_password = '#';
    $db_database = '#';
    $blog_url = 'http://localhost/experiment5/wordpress_284/index.php/'; //base folder for the blog. Make SURE there is a slash at the end
    $maxchars = 135;
    //connect to the database
    mysql_connect(localhost, $db_username, $db_password);
    @mysql_select_db($db_database) or die("Unable to select database");
    //get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
    $query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id ASC LIMIT 5"; 
    $query_result = mysql_query($query);
    $num_rows = mysql_numrows($query_result);
    //close database connection
    // html page starts after ?>
    //start a loop that starts $i at 0, and make increase until it's at the number of rows
    for($i=0; $i< $num_rows; $i++){ 
    //assign data to variables, $i is the row number, which increases with each run of the loop
    $blog_date = mysql_result($query_result, $i, "post_date");
    $blog_title = mysql_result($query_result, $i, "post_title");
    $blog_content = mysql_result($query_result, $i, "post_content");
    $blog_content = substr($blog_content, 0, $maxchars);
    $blog_content = $blog_content . "...";
    //$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.
    $blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format
    //format date
    $blog_date = strtotime($blog_date);
    $blog_date = strftime("%b %e", $blog_date);
    //the following HTML content will be generated on the page as many times as the loop runs. In this case 5.
    <li><strong><a href="<?php echo $blog_permalink; ?>"><?php echo $blog_title; ?></a></strong>
    		<?php echo $blog_content; ?> <a href="<?php echo $blog_permalink; ?>">(more)</a> <br />
    		<br /><br /></li>
    } //end the for loop

    You can adjust the length of the excerpt with the $maxchars = 135; variable.

    Works very well indeed.

    @investor – haha, you just totally bypassed WordPress’s query system. Good for you. 😉 I’m glad you got it working, though there should be a cleaner way… :/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Yet Another Related Posts Plugin] Showing more than one random post’ is closed to new replies.