How to get child category with post of parent category in WordPress

Default in WordPress category show all post in parent category or sub category. This tutorial will help you get sub category info with post and thumb of parent category in WordPress theme and group post by sub category

Step 1: Open archive.php file and insert code below

<?php get_header(); ?>
<div id="main-content" class="col-lg-12">
<?php get_sidebar();?>
<div class="col-lg-9 col-sm-8" id="blog-category">
<div class="">
<?php
  $args = array(
  'orderby' => 'name',
  'parent' => 2 //id of category, can use the_category_ID(false)
  );
$categories = get_categories( $args );
	foreach ( $categories as $category ) {
		echo '<div class="product-categories">
			<h3 class="module-title"><span>'.$category->name.'</span></h3>';
		echo '<div class="row">';
		include(TEMPLATEPATH. 'product-loop.php');
		echo '</div></div>';
	}
?>
</div>
</div>
</div>
<?php get_footer(); ?>

Step 2: Create product-loop.php and insert code below

<?php 
$args = array(
		'posts_per_page' => 6,
		'cat' => $category->cat_ID,
		'post_type' => 'post',
		'orderby' => 'title',
);
$the_query_product = new WP_Query( $args );
// The Loop
while ( $the_query_product->have_posts() ) {
	$the_query_product->the_post();
	?>

<div class="col-lg-4 col-sm-6 col-xs-6">
	<div class="product-block" itemscope itemtype="http://schema.org/Article">
		<a href="<?php echo permalink_link()?>">
		<?php the_post_thumbnail('full', array('class' => 'img-responsive','itemprop'=>'image')); ?>
		</a>
		<h4 class="entry-title" itemprop="name"><a href="<?php the_permalink(); ?>" itemprop="url"><?php the_title(); ?></a></h4>
	</div>
</div>

<?php 
}    
?>

Thanks for reading this post!

Share this :

Comments are closed.

Related post

Free Responsive Website templates

ThemeVina is Best site for free sharing and download Wordpress themes, Joomla templates, Responsive website templates and more HTML templates.

All template in Site are free. You can download and install for your site.

Contact us

  • Brand: Theme Vina
  • Email: admin@themevina.com
  • Website: www.themevina.com
  • Address: 367 Tran Hung Dao Street
  • Location: Thai Binh City, Thai Binh, Viet Nam

Newsletter