1

I have 2 categories in my cutom post type: Cat 1 Cat 2

I wish display a random category from this post type. I made this and it works:

function categorie(){    
    $global;    
    $args = array('type' => 'carte', 'taxonomy' => 'carte-category', 'parent' => 0);
    $categories = get_categories($args);
    shuffle( $categories);
    $i = 0;

    foreach($categories as $category) {
        $i++;
        $categoryname=  $category->name;
        $html.= '<h3> '. $category->name. '</h3>';
        $html.= '<p class="txtContent"> '. $category->description. '</p>'; 

        if (++$i == 2) break;
    }

    return $html;
}

add_shortcode( 'categorie', 'categorie' );

But now in this random category, I wish display a thumbnail from random post

To be clear:

Cat1 = Menus Posts: Menu 1, menu 2...

Cat 2 = Dishes Posts: Dish 1, Dish 2...

If my random cat is displaying Cat1, I need the thumbnail of Menu 1 or Menu 2

How can I do that ?

2 Answers 2

1

All you need to do is to add another query in your code:

function categorie_shortcode_callback() {    
    $html = '';
    $categories = get_categories( array(  
        // 'type' => 'carte', <- THERE IS NO ATTRIBUTE CALLED type FOR get_categories, SO REMOVE THAT
        'taxonomy' => 'carte-category',
        'parent' => 0
    ));
    shuffle( $categories);

    // you also don't need to loop through that array, since you only want to get first one
    if ( ! empty($categories) ) {
        $category = $categories[0];
        $html.= '<h3> '. $category->name. '</h3>';
        $html.= '<p class="txtContent"> '. $category->description. '</p>'; 

        $random_posts = get_posts( array(
            'post_type' => 'carte',
            'posts_per_page' => 1,
            'orderby' => 'rand',
            'fields' => 'ids',
            'tax_query' => array(
                array( 'taxonomy' => 'carte-category', 'terms' => $category->term_id, 'field' => 'term_id' )
            )
        ) );
        if ( ! empty($random_posts) ) {
            $html .= get_the_post_thumbnail( $random_posts[0] );
        }
    }

    return $html;
}
add_shortcode( 'categorie', 'categorie_shortcode_callback' );
0
-1

Inser this inside foreach loop:

$args_query = array(
    'order'          => 'DESC',
    'orderby'        => 'rand',
    'posts_per_page' => 1,
    'cat' => $category->term_id,
);

$query = new WP_Query( $args_query );

if( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        the_post_thumbnail();
    }
} else {
    echo 'No post found';
}

wp_reset_postdata();
4
  • thanks :) but it's not working I'm in function.php, maybe the query is different
    – nono
    Jan 14, 2019 at 14:15
  • How? Whay you are expect? And what you see?
    – wpdev
    Jan 14, 2019 at 14:17
  • It's displaying nothing. I'm in function.php, maybe the query is different ?
    – nono
    Jan 14, 2019 at 14:18
  • It won't work, because you use cat param in your query, and OP is using custom taxonomy and custom post type. Jan 14, 2019 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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