Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display Posts Category Wise
<?php
//Show post category wise shortcode [show_post_category_wise]
add_shortcode('show_post_category_wise','show_post_category_wise_shortcode');
function show_post_category_wise_shortcode(){
$terms = get_terms('category');
$html = '';
if($terms) {
foreach($terms as $term){
$html .= '<h2><a href="'.get_term_link($term->term_id).'">'.$term->name.'</a></h2>';
//get post
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'cat' => $term->term_id
);
$post_array = array();
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if ( $the_query->have_posts() ) :
$html .= '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post();
$html .= '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
endwhile;
$html .= '</ul>';
endif;
wp_reset_query();
}
}
return $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment