Created
August 28, 2018 13:06
-
-
Save navnit-viradiya/858200a0a63077aa1d7747e8d3762d43 to your computer and use it in GitHub Desktop.
Display Posts Category Wise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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