How to Display the Post by Category

In WordPress to display the posts in a template consider as easy. But in some of the cases if there is requirement to display the posts as per the category then it’s seems to be little different. In this tutorial we will show you how to display the posts as per the category.

//first define the taxonomy name using get_terms

<?php $terms = get_terms(array('lm_branch_state')); ?>

//start main foreach loop

<?php
$ark=1;
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'lm_branch_names',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'lm_branch_state',
'field' => 'id',
'terms' => $term->term_id
)
)
);
$postslist = get_posts( $args ); ?>
<div id="menu" class="tab-pane">
<div class="row">

//here display the category name

<div class="col-sm-12">
<div class="for-diver">
<div class="body-widget">
<h2 class="heading-title-new" id="#menu<?php echo $i; ?>"> <?php echo $term->name; ?></h2>
</div>
</div>
</div>

//display the posts in the inner loop

<?php foreach($postslist as $post){ setup_postdata($post); ?>
<?php
ob_start();
echo $content = ob_get_clean();?>
<div class="col-sm-3">
<div class="team-widget">
<div class="body-widget text-center">
<a href="<?php the_permalink(); ?>">
<?php if(has_post_thumbnail()) { ?>
<?php the_post_thumbnail('full'); ?>
<?php } ?>
<h3 class="name-title"><?php the_title(); ?></h3>
<?php the_content(); ?>
</a>
</div>
</div>
</div>
//end display the posts in the inner loop
<?php wp_reset_postdata(); } ?>
</div>
</div>

//end main foreach loop

<?php $ark++; } ?>
Find More Topics:

Get the client IP address using PHP
How To Auto Redirect Users After Logout In WordPress
How to Display the Post by Category
How to Create Our Own Shortcode in WordPress
How to Fix Internal Server Error on WordPress

2 thoughts on “How to Display the Post by Category

Comments are closed.