Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navnit-viradiya/214de86b2ead71a5849db15022f2d891 to your computer and use it in GitHub Desktop.
Save navnit-viradiya/214de86b2ead71a5849db15022f2d891 to your computer and use it in GitHub Desktop.
Show category wise posts for custom post type in wordpress
<?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