This is a custom theme…so in your theme’s index.php, is The Loop being initialized by query_posts() for some reason?
Aha!
<?php
if (is_home()) {
query_posts("cat=-3");
}
?>
That’s the culprit. But not to worry, as there’s a solution:
<?php
if (is_home()) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-3&paged=$paged");
}
?>
Let me explain what I’ve added, and why.
query_posts() is a powerful function, but in this situation it has a flaw: it overrides nearly everything in the standard posts object query, including what the paged offset is. To get proper pagination with query_posts() we need to recreate it through the ‘paged’ parameter or query. Best way to do this is to ask WordPress for the “page” we happen to be on, and use that as our ‘paged’ value.
The $paged =
line above uses what’s called a ternary operator to test whether the ‘paged’ query variable is available. If it is, $paged receives its value. If it isn’t, we assume we’re on the first page and assign it ‘1’. Then we tell query_posts() what page we’re on with the addition of &paged=$paged
.
Thank you so much 🙂 it works like a charm now, and thanks for taking the time to explain it in detail
I would like to implement the above snippet of code into an archive page which has an additional decorative loop with 3 posts of category 14.
What should the last line look like?
‘
<?php
if (is_archive()) {
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
query_posts(cat=-14&paged=$paged”); // how should this look like?
}
?>
‘
The code above does not work. My archive of category 9 contains only one post. But with this code a lot of other posts (it seems to be all of them) are shown.
Hope this pastebin doesn’t expire as fast as the previous one: http://pastebin.com/527946
As a sidemark: I wanted to hide my news loop on previous pages so news loop would only show up once on the home page. This is the code that worked for me:
<!-- START: if URL not paged, show news, else hide news -->
<?php if (!isset($_GET['paged']) && empty($_GET['paged'])) { ?>
<h2 class="news_head">The News</h2>
<?php include_once( 'my_loop_news.inc.php' ); ?>
<?php } ?>
<!-- END: if URL not paged, show news, else hide news-->
Thanks so much, Kafkaesqui! This problem has been giving me a headache and your code solved it perfectly.
Kafkaesqui, where exactly should your solution be placed? I’ve put it in the loop as follows with no success:
<?php endwhile; ?>
<?php
if (is_home()) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-6&paged=$paged");
}
?>
<?php else : ?>
I’m assuming that cat=-6' will exclude the cat. Any advice would be appreciated. BTW my loop starts out as follows:
<?php query_posts(‘cat=1,2,3,4,5,7&showposts=1’); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>`