I have a products
post type and it has a various categories. Currently there are around fifty plus posts assigned to different categories. Note: I am using a custom taxonomy called product_categories
.
Scenario:
- I have a category named
food-products
and it has a sub categories aschocolates, dairy, grocery
. - Now when user goes to the category page eg
site.com/product-category/food-products
at that time all other categories posts are also displaying but when i click on the sub category tabbing menu at that time it displays the correct assigned sub category posts
So, my question is how can the only the respected category of subcategory posts will be displayed on page load ?
<?php $loop = new WP_Query(array('post_type' => 'product', 'posts_per_page' => -1));
$count =0; ?>
<div class="row portfolio-container">
<?php if ( $loop ) : while ( $loop->have_posts() ) : $loop->the_post();
$url = get_the_post_thumbnail_url( $post_id, 'thumbnail_size' );
$terms = get_the_terms( $post->ID, 'product_categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) { $links[] = $term->name; }
$links = str_replace(' ', '-', $links);
$tax = join( " ", $links );
else :
$tax = '';
endif; ?>
<div class="col-lg-4 col-md-6 portfolio-item <?php echo strtolower($tax); ?>">
<div class="portfolio-wrap">
<img src="<?php echo $url;?>" class="img-fluid" alt="">
<div class="portfolio-info">
<h4><a href="<?php the_permalink() ?>"><?php the_title();?></a></h4>
<div class="portfolio-links">
<a href="<?php echo $url; ?>" data-lightbox="portfolio" data-title="App 1" class="link-preview" title="Preview"><i class="ion ion-eye"></i></a>
<a href="<?php the_permalink() ?>" class="link-details" title="More Details"><i class="ion ion-android-open"></i></a>
</div>
</div>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_query();?>
<?php endif;?>
</div>
JQuery:
$(window).on('load', function () {
var portfolioIsotope = $('.portfolio-container').isotope({
itemSelector: '.portfolio-item'
});
$('#portfolio-flters li').on( 'click', function() {
$("#portfolio-flters li").removeClass('filter-active');
$(this).addClass('filter-active');
portfolioIsotope.isotope({ filter: $(this).data('filter') });
});
});
Here is my working demo video for review.
food-products
and it has a sub categories aschocolates
,dairy
,grocery
. Now when user goes to the category page egsite.com/product-category/food-products
at that time all other categories posts are also displaying but when i click on the sub category tabbing menu at that time it displays the correct assigned sub category posts.... So, my question is how can the only the respected category of subcategory posts will be displayed on page load ? – TMA