WordPress.org

Forums

[resolved] WP_Query navigation issue with custom taxonomy (2 posts)

  1. michaelgodfrey
    Member
    Posted 2 years ago #

    Spent the day combing forums for the solution to this. Have 3 different post navigation setups at the bottom. Each has their own problems. First and foremost, I get 404s when the navigation takes me to the 3rd+ page in the query. Page one and two function as intended. I tried to add notes explaining what is happening.

    This is a child theme of twentytwelve.

    The page pulls four 'location' taxonomies into one page and displays the featured image with the post name in two columns. Just a light navigation page for a mobile theme. I could pull all nav and show all of the posts, but that would get download heavy.

    Any insight would be appreciated.

    <?php
    	//The Query - Pulls the 4 categories as planned.
    	//Currently totals 42 individuals.
    	//'posts_per_page' => -1, returns all 42, so the loop pulls everything properly.
    	$paged = (get_query_var('page')) ? get_query_var('page') : 1;
    	$args = new WP_Query();
    	$args->query( array(
    		'&paged='.$paged,
    		'post_type' => 'individuals',
    		'post_status' => 'publish',
    		'tax_query' => array(
    		'relation' => 'OR',
    			array(
                          	'taxonomy' => 'home-location',
    			'terms' => array('new-york','new-jersey','connecticut'),
                          	'field' => 'slug',
    			),
    			array(
                          	'taxonomy' => 'visiting-location',
    			'terms' => array('new-york'),
                          	'field' => 'slug',
    			),
    		),));
    ?>		
    
    <?php
    // My loop - Puts the featured image and the post title (individuals names) into two columns. Works fine.
    	$col = 0;
    	$col_count = 2;
    	$cols = array();
    	//if($args->have_posts()) : - commented out.  doesn't seem to make difference
    	while ( $args->have_posts() ) : $args->the_post();
    	if($col >= $col_count) $col = 0;
    	ob_start();
    ?>
    
    <div class="post" id="post-'<?php the_ID(); ?>'">
    	<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    	<div class="entry">
    	<?php
    		if ( has_post_thumbnail() ) {the_post_thumbnail( 'index-thumbnail' );
    		// the current post has a thumbnail
    		} else {
    		// the current post lacks a thumbnail
    		}
    		?>
    	</div>
    </div>
    <?php
    	$output = ob_get_contents();
    	ob_end_clean();
    	$cols[$col++] .= $output;
    	$count++; //one example had this. commenting out doesn't seem to make much difference
    	endwhile; // end of loop
    	//endif; - commented out.  doesn't seem to make difference
    	wp_reset_query();
    	is_home(); // end of loop - is_home() seems irrelevant, but was suggested somewhere
    ?>
    	<div class="columns">
    <?php
    		foreach($cols as $key => $col)
    		echo '<div class="column column' . $key . '">' . $col . '</div>';
    ?>
    	</div>	
    
    <?php
    // Three different variations of suggested navs.
    // One - This one adds page numbers to the nav.  1st page returns 10. 2nd page returns the next 10.  3rd and 4th pages error.
    // the url in the link does site/home-location/new-york/ for 1st page.  .../new-york/page/2/ for page 2 and .../new-york/page/2/page/3 for the non-working page 3.
    if($args->max_num_pages>1){?>
        <p class="pager">
        <?php
        for($i=1;$i<=$args->max_num_pages;$i++){?>
            <a href="<?php echo get_category_link($category_id).'page/'.$i;?>" <?php echo ($paged==$i)? 'class="active"':'';?>><?php echo $i;?></a>
            <?php
        }
        if($paged!=$args->max_num_pages){?>
            <a href="<?php echo get_category_link($category_id).'page/'.$i;?>">Siguiente</a>
        <?php } ?>
        </p>
    <?php } ?>
    
    <?php
    // Two - This one adds the traditional older/newer nav. 1st page returns 10. 2nd page returns the next 10.  3rd page errors.
    // the url in the link does site/home-location/new-york/ for 1st page.  .../new-york/page/2/ for page 2 and .../new-york/page/3/ for the non-working page 3.
    ?>
    
    <div class="navigation">
    	<div class="next-posts"><?php next_posts_link('« Older Entries', $args->max_num_pages) ?></div>
    	<div class="prev-posts"><?php previous_posts_link('Newer Entries »', $args->max_num_pages) ?></div>
    </div> 
    
    <?php
    // Three - This one adds number navigation.  Page 1 functions the same. All other pages return the same posts.
    // the url comes in .../new-york/?page=(page num) exmp: ?page=3
            if($args->max_num_pages>1){
              if ($paged > 1) { ?>
                <a href="<?php echo '?page=' . ($paged -1); //prev link ?>"><</a>
              <?php
              }
              for($i=1;$i<=$args->max_num_pages;$i++){?>
                <a href="<?php echo '?page=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
              <?php
              }
              if($paged < $args->max_num_pages){?>
                <a href="<?php echo '?page=' . ($paged + 1); //next link ?>">></a>
              <?php
              }
            }?>
  2. michaelgodfrey
    Member
    Posted 2 years ago #

    Much research showed that the only problem is that in the reading section on the dashboard, one must match the posts_per_page with the 'blogs pages show at most' setting.

    This also made a difference:

    $paged = 1;
    if ( get_query_var('paged') ) $paged = get_query_var('paged');
    if ( get_query_var('page') ) $paged = get_query_var('page');

    I got different behaviors out of the 3 different nav types base on the value being paged vs. page.

    I looked for a method to detach the 'blogs pages show at most' from the posts_per_page setting. I was hoping to have the number displayed to be different on standard and mobile themes. This showed lots of promise, but I never managed to get it to implement right:

    http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/

    So for now I'll use the one global setting.

Topic Closed

This topic has been closed to new replies.

About this Topic