0

I am creating a slider that loads 5 different categories. The categories are randomly loaded into the page. So I know the id's of the categories and they are loaded random to the page.

What I want to do now is load the latest post corresponding to that category.

Here is what I have so far:

<?php 
    $numbers = Array('5','6','7'); //id's of the categories
    shuffle($numbers);
    query_posts('cat=5&posts_per_page=1'); 
    while (have_posts()) : the_post();                  
    foreach ($numbers as $number) {                         
        echo '<div class="carousel_items">
        <img src="'.get_bloginfo('template_directory').'/images/'.$number.'.svg" /><br />
            '.get_the_excerpt().'
        </div>';
    }
    endwhile; 
?>

So this loads the latest post from categorie 5. And repeats itself by every step. This is the problem. I can't do the query_posts in the foreach loop because that creates an endless loop.

Any ideas how to fix this?

  • Are you just doing 1 loop of a random category from the array or are you querying all of the categories? You should use WP_Query instead of query_posts – Howdy_McGee Nov 16 '15 at 16:08
  • There is one loop that loads all from cat 5, but I want to load from different categories. The ones in the array to be precise. Thnx for the link. I'll read up. – Interactive Nov 16 '15 at 16:11
3

Never ever use query_posts, it creates an endless amount of issues as it breaks the main query object on which so many build in functions, custom functions and plugin rely on. It is pure evil and you should avoid it as such as well.

For custom queries, use WP_Query or get_posts, if you simply need to alter the current main query (except on single pages, static front pages and true pages), use pre_get_posts.

Your query is completely wrong and you are not doing anything with your random category number. Lets look at your code, rewritten; (NOTE: Requires PHP 5.4+)

$numbers       = ['5','6','7']; //id's of the categories
// Get a random key from the array
$rand_key      = array_rand( $numbers );
// Get a random category ID from the array
$random_cat_ID = $numbers[$rand_key];

// Now build our query args
$args = [
    'cat'            => $random_cat_ID,
    'posts_per_page' => 1
];
$q = new WP_Query( $args );
// Run the loop
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();

        // Display what you need from the loop like title, content etc

    }
    wp_reset_postdata();
}

EDIT

If you need to shuffle the array of category ID's in $numbers and then get the latest post from each category, we need a different approach here. You need to realize that you will need to run a query for each category. There is really not much you can do about this because you will be using random ordering.

Lets look at the code:

$numbers       = ['5','6','7']; //id's of the categories
// Shuffle the array
$rand_key      = shuffle( $numbers );

// Run a foreach loop to get the newest post from the categories
foreach ( $numbers as $cat_id ) {
    $args = [
        'posts_per_page' => 1,
        'cat'            => $cat_id,
        'no_found_rows'  => true // Legally skips pagination
        // Add any extra arguments
    ];
    $q = new WP_Query( $args );

    // Run the loop
    if ( $q->have_posts() ) {
        while ( $q->have_posts() ) {
        $q->the_post();

            // Display what you need from the loop like title, content etc

        }
        wp_reset_postdata();
    }
}
  • Nice. Thank you. But I'm not there yet. This might be my fault for not being clear enough. The idea is that every category is represented and the latest post of the category is shown. So the numbers are category id's. This query shows one category random. I hope this doesn't make things more complicated. – Interactive Nov 20 '15 at 8:14
  • I'm a bit confused here, should $numbers be an array of all category ID's then you randomly select a category and get a post from that ID, or should $numbers be an array of category ID's and then you get a random post from every category that is in the array – Pieter Goosen Nov 20 '15 at 10:05
  • Yes I can understand you are. Sorry. The $numbers array are categories that are from som specific categories. Then when the page loads get the latest post from every category in the array! – Interactive Nov 20 '15 at 10:27
  • How are you getting $numbers, are this static – Pieter Goosen Nov 20 '15 at 10:31
  • I gave the categorie id's myself – Interactive Nov 20 '15 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.