Exclude category and post from loop in custom category.php
-
14-04-2021 - |
Question
So I created a custom category.php where I have the following loop:
<div id="category__product-grid" class="row">
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="col-md-4 col-product-item">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="cat__featured-img"><?php the_post_thumbnail(); ?></div></a>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<?php endwhile;
else: ?>
<p>Sorry, no posts matched your criteria.</p>
</div>
I broke my head trying to understand how can I exclude the needed categories from this loop and the required posts as well. Would appreciate any help!
Solution
Here is a correct way:
<div id="category__product-grid" class="row">
<?php query_posts( array( 'cat' => array(6,-45), 'post__not_in' => array(-598), 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="col-md-4 col-product-item">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="cat__featured-img"><?php the_post_thumbnail(); ?></div></a>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<?php endwhile;
else: ?>
<p>Sorry, no posts matched your criteria.</p>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange