Created
August 28, 2018 12:57
-
-
Save navnit-viradiya/214de86b2ead71a5849db15022f2d891 to your computer and use it in GitHub Desktop.
Show category wise posts for custom post type in wordpress
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('technology'); | |
$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' => 'our_work', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'tax_query' => array( | |
array ( | |
'taxonomy' => 'technology', | |
'field' => 'id', | |
'terms' => $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