0

Site Links: http://manofmany.com & http://manofmany.com/page/2/

I'm seeing my 2nd page displaying the exact same posts as my first page (minus the very first/latest post). All other pages 3,4,5 etc are performing as normal/expected. I was changing around the coding to exclude a category, so I tried copying in the original coding to fix it but with no luck. Here is the start of the loop:

<?php if( is_front_page() && !is_paged() ) : ?>
<div style="border-bottom: #c4c4c4 1px solid; margin-bottom: 20px; padding-bottom:20px;">
<?php dynamic_content_gallery(); ?></div><?php endif;?>

<?php global $query_string; query_posts( $query_string . '&cat=-1470'); ?>
<?php if (have_posts()) : $i = 1; while ( have_posts() && $i <= $loop ) : the_post(); ?>

I've also looked at this but can't quite get it to work: Wordpress second page displays the same content as my first

Most other errors and solutions I find are in relation to all pages (3,4,5) displaying the same posts as the first. I'm only experiencing the problem with my second page.

Detail instructions are appreciated as I'm by no means an expert at dealing with php.

1 Answer 1

2

Solution 1

It looks like this might be what is happening to your template: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

Basically what it's saying is that the second page doesn't realize it's the second page because the "paged" parameter is missing.

I've just copied directly from the wordpress documentation, which says you should do this:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=3&paged=' . $paged); 
?>

So in your code, you might try to replace this line:

<?php global $query_string; query_posts( $query_string . '&cat=-1470'); ?>

With the following:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $query_string;
query_posts($query_string . '&cat=-1470&posts_per_page=3&paged=' . $paged); 
?>

You should replace '3' with however many posts per page you would like.

Solution 2

The first solution may not be the best way to accomplish what you want, but it should get the job done. For a better way, you can look into using the pre_get_posts filter to exclude a certain category, and this should not have the side-effect of breaking pagination. Here is a reference to that: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_categories_on_your_main_page

That code is pretty straightforward; you should be able to simply put it into functions.php in your theme, and you're done:

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

So, there you have it, two possible solutions to the problem. I'd try the second one first...

6
  • Thanks, I'll give it a go soon and let you know what the outcome is! Commented Sep 23, 2013 at 3:43
  • +1 for pre_get_posts, query_posts() is extremely unreliable with pagination
    – Rarst
    Commented Sep 23, 2013 at 9:38
  • @Eugenious So I was able to fix the pagination but am unable to exclude the category 1470 from the front page using either code. Here's my code now: <?php if( is_front_page() && !is_paged() ) : ?> <div style="border-bottom: #c4c4c4 1px solid; margin-bottom: 20px; padding-bottom:20px;"> <?php dynamic_content_gallery(); ?></div><?php endif;?> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('posts_per_page=10&paged=' . $paged); ?> <?php if (have_posts()) : while ( have_posts() ) : the_post(); ?> Commented Sep 24, 2013 at 11:16
  • Nevermind. I fixed it with: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts('posts_per_page=10&cat=-1470&paged=' . $paged); ?> I would still like to be able to use pre_get_posts however if you think this is a better/cleaner option Commented Sep 24, 2013 at 11:28
  • pre_get_posts is a cleaner option. give it a try!
    – Eugenious
    Commented Sep 25, 2013 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.