mardi 24 avril 2018

WordPress: Show random 4 categories with 3 last posts

I am busy to build a row that will show randomly 4 categories with the category title and the 3 latest posts under it. But i am stuck how i can get the categories in random order because the 'orderby' is not working...

Can somewone help me with that?

The code i am using:

 <?php
    //for each category, show all posts
    $cat_args   = array(
        'orderby' => 'rand',
        'order' => 'ASC'
    );
    $limit      = 4;
    $counter    = 0;
    $categories = get_categories($cat_args);

    foreach ($categories as $category):
        if ($counter < $limit) {
            $args  = array(
                'showposts' => 3,
                'category__in' => array(
                $category->term_id
            ),
            'caller_get_posts' => 1
        );
        $posts = get_posts($args);
        if ($posts) {
            echo '<h3><a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . '</a> </h3>';
            foreach ($posts as $post) {
                setup_postdata($post);
?>

<p><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

<?php
            } // foreach($posts
        } // if ($posts
    } // foreach($categories
?>

<?php
    $counter++;
    endforeach;
?>




Aucun commentaire:

Enregistrer un commentaire