$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - display a random post thumbnail from a random category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - display a random post thumbnail from a random category

matteradmin8PV0评论

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 ?

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 ?

Share Improve this question edited Jan 14, 2019 at 14:39 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Jan 14, 2019 at 13:49 nononono 333 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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' );

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();
Post a comment

comment list (0)

  1. No comments so far