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 badges2 Answers
Reset to default 1All 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();