• I’ve searched through and can’t seem to find this, although there are plugins for creating random posts, they make no sense (and have no details on how to implement them).

    I’m putting my blog in the morque, so all I want to do is make the front display one random post (1.5 allows one post, just not random).

    Thanks.

Viewing 15 replies - 1 through 15 (of 23 total)
  • Hmm. You can initialize The Loop to display a single post by inserting
    query_posts() just before it with an argument of "p=ID" — ID being replaced with the numeric ID of a post. To pass it a random post ID requires a way of retrieving the ID randomly. So try this as your query_posts() code:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>

    Thread Starter gvtexas

    (@gvtexas)

    Thanks, but I have no clue how to implement this. I tried this code, which results in no error, but no post shows either! Do I have it in the wrong place?

    <?php if (have_posts()) : ?>

    <?php<br />
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");<br />
    query_posts("p=$rand_id");<br />
    ?>

    <!-- ?php while (have_posts()) : the_post(); ? -->

    <div class="post"><br />
    <h2 id="post-<?php the_ID(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/bug-head.jpg"> <a>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

    <div class="entry">

    <?php the_content('Read the rest of this entry &raquo;'); ?><br />
    </div>

    <p class="postmetadata">posted <?php the_time('F jS, Y') ?> in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong>|</strong>'); ?> <?php comments_popup_link('no comments', '1 comment', '% comments'); ?> <strong>|</strong> <a>/wp-print.php?p=<?=the_ID()?>">print version</a>

    </div>

    <!-- ?php endwhile; ? -->

    <div class="navigation">

    <?php posts_nav_link('','','&laquo; earlier posts') ?><br />
    | <a>">home</a> | <?php posts_nav_link('','newer posts &raquo;','') ?>

    </div>

    <?php else : ?>

    <h2 class="center">Not Found</h2><br />
    <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?><br />
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>

    <?php endif; ?>

    </div><br />

    Might I suggest my random posts plugin? It’s a single template tag that you insert into your templates. You can select how many random posts to display (you want just one); as well as which categories to use (or zero for all categories).

    So, you could do this in your index.php:

    <?php
    $p = random_posts(0, 1);
    query_posts("p=$p");
    // run through the Loop as normal now

    See the query_posts() documentation for additional details.

    You’ve got the following two lines commented out:
    <!-- ?php while (have_posts()) : the_post(); ? -->
    <!-- ?php endwhile; ? -->

    Those are the two lines that actually loop through and display each individual post. They should be:

    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>

    That should get the posts displaying. Skippy’s plugin also works quite well.

    My code would also go before The Loop, as I note above, and not *in* it:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>
    <?php while (have_posts()) : the_post(); ?>

    ~Post-related code and template tags go here. ~

    <?php endwhile; ?>

    Thread Starter gvtexas

    (@gvtexas)

    Thanks to all. I got it working, Karfkaesqui, based on your last post suggestion. Works really well (now!). ??

    wild26

    (@wild26)

    I’m trying to do this single, random-post thing too, but I can’t get Skippy’s plugin to work. When I place the few lines of code just above “the loop” I just get an error message.

    Do I have to comment out some other lines to make it work? Which ones?

    I am using the code quite similar as described by Kafkaesqui above, however, it does show the entire content, without the <!–more–> tag and it neither shows the comment count. Anyway to implement this?

    Oh yeah, I’m also trying to exclude a category… however just coding
    ... WHERE post_status = 'publish' AND post_category != 1 ...
    does not seem to work whatsoever. It actually only shows one post all the time that actually has that category number.

    What if I want a link that says “random” on my page and when you click it takes you to a random post or page. The above listed comments seem to deal with displaying in a template. I just want a link that jumps to a random page or post.

    @ njg97r: just include the permalink instead of all the post data. Something like this should work for you:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>
    <?php while (have_posts()) : the_post(); ?>

    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">random</a>

    <?php endwhile; ?>

    Yes, you load alot of the db, but since PHP and MySQL is so fast, that wouldn’t be a problem at all. Everytime the page reloads, you’ll get a new “random” link (basically, the permalink to the individual post).

    CounterDax: That code for just a random permalink is exactly what I was looking for. Works perfectly, too. Thanks!

    I think my problem is solved… I should’ve called setup_postdata($post); too.

    Hi

    I have used the hack that Kafkaesqui proposed on this site:
    https://www.weddingcompanycornwall.co.uk
    It is the thing below the tips section. It is working brilliantly except for one thing. I only want to display part of the post and then link to the rest of the post. For the moment I am including the post title so that is a link, but how do I make those … at the end of the post become a link please?

    Thanks

    Rich

    Well, first of all, the link leads to a completely unrelated website: you trying to sneak in some advertisement?

    Can you post the snippet of code here? I recon the ‘…’ in your post means the ‘[…]’ when calling the excerpt… are you calling the excerpt? If this is the case you need to hack WordPress itself.

    /wp-includes/functions-formatting.php
    Locate:

    function wp_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = $post->post_content;
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = 55;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '[...]');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }

    Change:

    function wp_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = $post->post_content;
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = 55;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '
    <a href="' . get_permalink() . '">...</a>');
    $text = implode(' ', $words);
    }
    } else {
    $text = $text . '... <a href="' . get_permalink() . '">more</a>&raquo;';
    }
    return $text;
    }

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘One Random Post (not random list or link)’ is closed to new replies.