$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Exclude category and post from loop in custom category.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Exclude category and post from loop in custom category.php

matteradmin8PV0评论

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!

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!

Share Improve this question asked Dec 8, 2018 at 15:50 JamdevJamdev 1175 bronze badges 2
  • did you try pre_get_posts? – rudtek Commented Dec 8, 2018 at 16:16
  • @rudtek no, could you, please, let me know more details? – Jamdev Commented Dec 8, 2018 at 16:25
Add a comment  | 

1 Answer 1

Reset to default 0

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>  
Post a comment

comment list (0)

  1. No comments so far