• I’ve been searching through the codex for a solution and have yet to find something to suit my needs. Basically what I’m looking to do is run 2 different loops in index.php, one for categories 8 and 9 and one for all the other categories. At this point I am able to display 3 posts in the one loop, however the only sort options for get_posts are ascending or descending order.

    I tried a few plugins but haven’t been able to get them to display more than just the title. Without further delay, here is the code.

    You can see what I’m trying to do, obviously “orderby=rand()” does not work. So now it just shows the last 3 posts by entry date. Does anyone have suggestions?


    <?php
    query_posts ('cat=8,9&showposts=3&orderby=rand()');
    if ( have_posts() ) :
    while ( have_posts() ) : the_post();
    ?>
    <div class="page-intro">
    <img src="../images/intro<?php the_ID(); ?>.jpg" />
    <h2><?php the_title(); ?></h2>
    <div class="intro-excerpt"><?php the_excerpt(); ?></div>
    </div>
    <?php
    endwhile;
    endif;
    ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,
    I found this plugin in a mailinglist of wp hackers somewhere:

    <?php
    /*
    Plugin Name: Random Posts Query
    */
    function query_random_posts($query) {
    return query_posts($query . '&random=true');
    }
    class RandomPosts {
    function orderby($orderby) {
    if ( get_query_var('random') == 'true' )
    return "RAND()";
    else
    return $orderby;
    }
    function register_query_var($vars) {
    $vars[] = 'random';
    return $vars;
    }
    }
    add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
    add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );
    ?>

    Install this as a plugin, activate it, and then place this above the loop (note the random=true at the end:

    <?php
    query_posts('cat=22,3,2,5&showposts=3&random=true'); ?>

    It works well for me!

    thanks rembem, you save me a lot of time;))) it works very well, finally easy to use and customize random plugin

    It must be something obvious but I can’t find it ! So I call some help. Sometimes you are searching your glasses and they are on your nose, isn’t it ?

    I have made a dropdownmenu witch list each category on my blog with :

    <? wp_list_cats(‘sort_column=name&exclude=7’); ?>

    When you click on “Books”, you arrive on the category page. This one obeys to the archive template and so display the list of all the posts in this particular category. Easy until this point.

    My problem is that I want too to display 2 posts (with title, texte and all stuff) of this category below this list… randomly !

    So I have installed this wonderfull plugin (works like a charm) : https://www.ads-software.com/support/topic/70359?replies=3

    And I have put like it is advised this line in my archive template :

    <? query_posts(‘cat=&showposts=2&random=true’); ?>

    2 posts are displayed randomly… but I can’t find how to indicate to the query that she must take these posts in the current category… and I don’t want to make 11 different templates… There must be a solution.

    I have try is_category() whithout success, some pieces of code about current category in the codex and it doesn’t work. I have search on the forum and found nothing about my problem.

    Is there someone to save me and the world ?

    THanks in advance for your attention.

    Pixelboss, I guess you don’t need a plugin to do it.

    You could use the WordPress get_posts() function and use the orderby parameter do use a random value.

    Take a look at this code:

    get_posts('numberposts=4&category=3&orderby=rand()');

    Regards,
    Caio Proiete
    https://www.pdaexpert.net

    I made a plugin for a buddy of mine that displays random posts by category.

    Basically, it has three functions, can display a set number of posts that belong to two distinct categories(eg only displays posts that belong to category 1 and category 5), display a set number of random posts that belong to two distinct categories, or a set number of random posts that belong to a single category.

    https://www.ax697.org/46/category-magic-plugin/

    Maybe this can help, not sure.

    oh, my god, finally the solution to what I’ve wanted to get done… pick a category and show a limited number of posts (most recent or random) from it in the format of my choice!

    This one is brilliant!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display Random Posts from Specific Categories’ is closed to new replies.