• Resolved skeg64

    (@skeg64)


    I thought this might help anyone else looking to display one random sticky post from a list of all sticky posts (eg. for the front page):

    <?php $sticky=get_option('sticky_posts') ; $randomStickyNo=(rand()%(count($sticky))); query_posts('p=' . $sticky[($randomStickyNo)]); ?>

    ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter skeg64

    (@skeg64)

    I have improved the code to work on pages with multiple loops:

    <?php $sticky=get_option(‘sticky_posts’); $randomStickyNo=0; $randomStickyNo=(rand()%(count($sticky))); $postno = $sticky[($randomStickyNo)]; $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); $wp_query->query(‘p=’ . $postno); ?>

    I was getting very strange behavior due to the multiple loops. Some browsers (Chrome, Firefox) would display the correct post, and IE6, IE7, Safari would return as “not found”.

    Btw, How to display only 5 sticky posts randomly, assumed I have 10 sticky posts, and I want to display only 5 sticky posts randomly.

    I’ve used this code before:

    <?php query_posts(array(‘post__in’=>get_option(‘sticky_posts’), ‘orderby’ => ‘rand’, ‘showposts’ => 5)); while (have_posts()) : the_post(); ?>

    but it’s not work. What I get using this query is 10 sticky posts displayed, not 5 posts. ‘showposts’ isn’t work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Solution to display one random Sticky Post’ is closed to new replies.