I have this problem: i have made a custom page with his own set of categories. In a shortcode i want to get all the categories and in the categories i want all the post related to that category.
function innovatiewerkplaats_sort($atts, $content = null){
global $post;
$terms = get_terms('innovatiewerkplaats_categories'); // Get all terms of a taxonomy
$nieuws = '';
foreach($terms as $term):
$nieuws .= '<div class="one">
<h2>Thema <strong>'.$term->name.' id='.$term->term_id.'</strong></h2>
<div class="wrapper">';
$category_query_args = array(
'post_type' => 'innovatiewerkplaats',
// 'category' => $term->term_id,
'category_name' => $term->name,
// 'cat' => $term->term_id,
);
query_posts($category_query_args);
if( have_posts() ) : while (have_posts()) : the_post();
$post_home= get_the_post_thumbnail( $page->ID, 'post-home');
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
$excerpt = get_the_content();
$excerpta = preg_replace("~(?:[/?)[^/]]+/?]~s", '', $excerpt);
// $ter = wp_trim_words( apply_filters( 'rpwe_excerpt', $excerpta ), $args['length'], 'â¦' );
$nieuws .= '<a href="'.get_permalink().'" title="'.get_the_title().'" class="one_fourth workplace">'.$term->term_id.'<span class="img" style="background:url('.$url.') no-repeat center center; background-size:cover;"></span><span class="titel">'.get_the_title().'</span><p></p><span class="more">Lees meer</span></a>';
endwhile; endif;
$nieuws .='</div></div>';
endforeach;
return $nieuws;
}
Note: Don’t use
query_post
. UseWP_Query
orget_posts
.Please take a look discussion When to use WP_query(), query_posts() and pre_get_posts.
Here sample approach related to your issue to generate all posts base on their category, then display via shortcode for later.
wp_query
orget_posts
),get_the_terms
.simple loop.
add_shortcode
( this code use simple shortcode only ).You can change element structures as your needs. As note, with this code, no duplicate posts in each category, and make sure you change the values such as post type and taxonomy as commented in code. I hope this helps.