Title: Movenext in loop without counting
Last modified: August 19, 2016

---

# Movenext in loop without counting

 *  [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/)
 * Hi all,
    not sure if this is possible or not but here’s what I’m trying to do:
   I have:
 *     ```
       <?php elseif (in_category(48)) : ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       ```
   
 * …etc, etc
 * This loops through 10 times and outputs in 2 lines of output with the post excerpt
   etc.
 * What i’m trying to do is exclude posts from certain categories within the loop,
   which is easy enough, but i DON’T want the loop to count. i.e. it outputs 4 then
   comes across one I want to exclude and it skips over it but will still output
   a further 6.
 * at the moment it’s looping through and just showing 9 as i’m skipping one because
   I don’t want to display, which is half-right.
 * Hope i’ve explained that ok!

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/movenext-in-loop-without-counting/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/movenext-in-loop-without-counting/page/2/?output_format=md)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870748)
 * Not quite sure, but I think this is what you want. Lets say you want to exclude
   category 12:
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post();
          if (in_category(12)) continue;
       ?>
       ```
   
 * The main problem with this is that if all posts on the page are category 12, 
   you will get a blank page.
 *  Thread Starter [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870828)
 * Thanks vtxyzzy that’s what I’ve got but as you said, the potential is i could
   be left with a blank page. Is there no way to skip them but keep the number that
   i’m currently on so i don’t end up with a blank page?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870854)
 * The way to do that is to modify the query. I can’t be specific without seeing
   your code, but it should be something like this:
 *     ```
       global $wp_query;
       query_posts(
          array_merge(
             $wp_query->query,
             array('cat' => -22),
          )
       );
       ```
   
 * where 22 is the id of the category you want to exclude. The query would be followed
   by the `if (have_posts())` test.
 *  Thread Starter [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870882)
 * hi vtxyxxy,
 * thanks for your help, i managed to try this today and get an error:
    `Parse error:
   syntax error, unexpected ')' in C:\xampplite\htdocs\wordpress3-1\wp-content\themes\
   themename\loop.php on line 94` line 93,94 & 95 are:
 *     ```
       array('cat' => -55),
       )
       );
       ```
   
 * if i take out the comma then the code compiles but it’s not taking the category
   away. full code is below if it helps, this is all in the loop.php file.
    I probably
   haven’t explained things very well so i’ll try again. Basically i have a category(
   48=news) and (55=sport). In a nutshell i’m fudging my RSS feed, so i sometimes
   want ‘sport’ items to be in the ‘news’ RSS feed but ignored by the site front
   end. it may be easier to do it from the RSS side but I know less about RSS then
   i do about PHP (which is saying something!)
 *     ```
       <?php elseif (in_category(48)) : ?>
        <?php $c = 0 ; ?>
         <div id="container_division">
          <?php
          global $wp_query;
          query_posts(
           array_merge(
            $wp_query->query,
             array('cat' => -55),
            )
           );
          ?>
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
           <?php $c++;
            if ($c == 1 or $c == 2 or $c == 5 or $c == 6 or $c == 9 or $c == 10) {
             $extra_class = 'Even';
            } else {
             $extra_class = 'Odd';
            } ?>
           <div class="post-block<?php echo $extra_class; ?>">
            --output image, excerpt etc
           </div>
          </div>
          <?php endwhile; else: ?>
          <?php endif; ?>
          </div>
       <?php wp_reset_query() ?>
       ```
   
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870884)
 * Sorry for the extra comma – betrayed by the flying fingers!
 * The code you have looks OK, but I am curious about the first line:
 * `<?php elseif (in_category(48)) : ?>`
 * That indicates to me that this code is only executed when the current post is
   category 48, so I wonder if the code is being executed at all.
 * To find out, put in this line just after that one:
 * `<?php echo "<h2>TESTING</h2>"; ?>`
 *  Thread Starter [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870887)
 * hi, yes it is being executed. It isn’t necessary on reflection (though to get
   this work it probably would) but its in there and works, just probably not very
   efficient.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870888)
 * I don’t know what else to try. The code looks correct for excluding the category.
   Are posts from that category still showing up?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870890)
 * what template are you working at?
    can you paste the whole code of the template
   into a [http://wordpress.pastebin.com/](http://wordpress.pastebin.com/) and post
   the link to it here?
 * also, have a look into ‘category__not_in’.
    [http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters](http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters)
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870895)
 * I am still curious about the in_category() call. That is normally used either
   in the loop, or in templates where a single post is shown, not a category archive.
 * If you are testing for a category archive, you want to use is_category() instead.
 *  Thread Starter [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870896)
 * Hi all, sorry was busy over the weekend.
    vtxyzzy – yes the posts from sport 
   are still showing up and all the code is within the “loop.php” file which is 
   called using the “get_template_part” call. alchymyth – thanks the “category_not_in”
   looks like that might do it, i’ll have a try when i’ve chance later and let you
   know but it seems to be exactly what I’m after!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870897)
 * I think the problem is that you have created a loop within the main loop. There
   is a ‘while (have_posts())’ line near the top of loop.php that starts the loop.
   Your elseif statement seems like it is in the middle of the loop, and your query
   then would be a second loop.
 * Since I can’t see all of the code, this is somewhat of a guess, but I think you
   need to move your category restriction to a point before the main loop starts.
 * Put your code in a pastebin, as alchymyth says, and post a link to it here.
 *  Thread Starter [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870901)
 * ok, full file (except for printing the stuff to the screen) is up on pastebin,
   think i’ve done it right: [http://wordpress.pastebin.com/B5J7HD0r](http://wordpress.pastebin.com/B5J7HD0r)
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870903)
 * I am confused by all the separate (almost identical) loops. Please explain what
   you want to happen, not what you are trying to code.
 *  Thread Starter [jdku](https://wordpress.org/support/users/jdku/)
 * (@jdku)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870909)
 * yeah, as i mentioned before, on reflection their completely un-needed I just 
   haven’t got round to taking them out.
    Within the parent category of ‘news’ there
   are 5 or so sub-categories, occasionally i want to say that a ‘news’ item is 
   also a ‘sport’ item (another parent category). However I don’t want the sport
   item to appear on the website front end under news, ONLY in the RSS feed (as 
   this is picked up by some news agregator sites that send quite a bit of traffic).
   But, if i just skip the ‘sport’ item (which is easily done) i’m left with a blank
   space (only 10 items are displaying on the front end before ‘previous’ and ‘next’
   buttons appear), I want to be able to skip the ‘sport’ item on the front end 
   but not have that blank space. so if out of 12 items, 10 are news and 2 are sport,
   then the RSS feed will show them all, but the front end would just ignore the
   2 sport and simply show 10 news items and not go over 2 pages. probably haven’t
   explained it any better!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/#post-1870910)
 * Try changing the array merge just a little:
 *     ```
       <?php elseif (in_category(48)) : ?>
          <?php $c = 0 ; ?>
          <div id="container_division">
             <?php
             global $wp_query;
             query_posts(
                   array_merge(
                     $wp_query->query,
                     array('cat' => '48,-55'))
             );
             ?>
             <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <?php $r = ++$c % 4;
                if ($r == 1 or $r == 2) {
                   $extra_class = 'Even';
                } else {
                   $extra_class = 'Odd';
                } ?>
                <div class="post-block<?php echo $extra_class; ?>">
                   //print the stuff out
                </div>
             <?php endwhile; else: ?>
             <?php endif; ?>
          </div>
          <?php wp_reset_query() ?>
       ```
   
 * I also tweaked the Even/Odd code so it should work if you ever want to change
   the posts_per_page to something other than 10.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/movenext-in-loop-without-counting/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/movenext-in-loop-without-counting/page/2/?output_format=md)

The topic ‘Movenext in loop without counting’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 3 participants
 * Last reply from: [jdku](https://wordpress.org/support/users/jdku/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/movenext-in-loop-without-counting/page/2/#post-1870927)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
