Support » Plugins » Hacks » Custom Post Type Archive Page – Pagination Isn't Working

  • Resolved doodlz

    (@doodlz)


    I created a custom post type and have displayed them on a custom archive page. I tried adding pagination but the pagination does not show up. Somehow the links are blank. When I preview the source code in the browser via Firebug, it shows the surrounding navigation div, but the individual post link divs are “grayed out.” Somehow the post links are not registering. My custom archive page code is below. Note: I’m using the Genesis framework.

    <?php
    /*
    Template Name: Press-Posts
    */
    
    remove_action('genesis_loop', 'genesis_do_loop');//remove genesis loop
    remove_action('genesis_sidebar', 'genesis_do_sidebar');
    remove_action('genesis_sidebar_alt', 'genesis_do_sidebar_alt');
    remove_action('get_header', 'ss_sidebars_init');
    add_action('genesis_sidebar', 'include_press_sidebar');
    add_action('genesis_loop', 'press_loop');//add the press loop
    
    function press_loop() {
     	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $loop = new WP_Query( array( 'post_type' => 'press_post', 'posts_per_page' => 10, 'paged'=>$paged ) ); ?>
        <h1><?php the_title(); ?></h1>
        <div id="press-post">
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div id="post-<?php the_ID(); ?>" class="press">
            	<h2><?php the_title(); ?></h2>
                <h3><?php echo genesis_get_custom_field('press-date', true ); ?></h3>
                <?php genesis_post_content(); ?>
                <?php $pressarticlelink = genesis_get_custom_field('press-article-link', true );
                	if ($pressarticlelink) {
                	?>
                	<a class="page-more-link" href="<?php echo $pressarticlelink; ?>" title="Press Article Permalink"><?php echo genesis_get_custom_field('press-link-text', true ); ?></a>
                	<?php } else { ?>
                		<! -- Displays blank -->
                	<?php } ?>
            </div>
        <?php endwhile;?>
        </div><!--end #press-post -->
        <div class="navigation">
            <div class="alignleft"><?php next_posts_link('Previous Entries'); ?></div>
            <div class="alignright"><?php previous_posts_link('Next Entries'); ?></div>
        </div>
        <?php
        }
    
    genesis();

Viewing 5 replies - 1 through 5 (of 5 total)
  • The problem i think is that next_posts_link and previous_posts_link both look at the $wp_query object to determine how many pages there are.

    http://core.trac.wordpress.org/browser/tags/3.0.4/wp-includes/link-template.php#L1474

    What i’d suggest, as an alternative to having to modify the main query object is to temporarily update the necessary $wp_query property prior to calling the post link functions, then setting it back to it’s original value thereafter.

    Something along these lines should hopefully work for you.

    <div class="navigation">
    	<?php
    	// Bring $wp_query into the scope of the function
    	global $wp_query;
    
    	// Backup the original property value
    	$backup_page_total = $wp_query->max_num_pages;
    
    	// Copy the custom query property to the $wp_query object
    	$wp_query->max_num_pages = $loop->max_num_pages;
    	?>
    
    	<!-- now show the paging links -->
    	<div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
    	<div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
    
    	<?php
    	// Finally restore the $wp_query property to it's original value
    	$wp_query->max_num_pages = $backup_page_total;
    	?>
    </div>

    Hope that helps..

    Thread Starter doodlz

    (@doodlz)

    Thank you so much, that works like a charm!

    Now I’m trying to get my around it so I can understand why it wasn’t working before. I understand how the post link functions look to $wp_query to determine the page count. So is it because I have a custom loop that $wp_query doesn’t hold that information for the post links? (Sorry for the dumb question, I’m just beginning to learn PHP.)

    So is it because I have a custom loop that $wp_query doesn’t hold that information for the post links?

    That’s right, the two nav functions both look at $wp_query and not your custom $loop object, the above code simply works around that by temporarily updating the appropriate property inside $wp_query(max_num_pages in this case) then setting it back after the links have been output.

    Thread Starter doodlz

    (@doodlz)

    Okay, right. Thanks so much for your help!

    Dee Teal

    (@thewebprincess)

    I’ll point out at this point, for anyone playing along at home that the nav links have an error

    <div class=”alignleft”><?php next_posts_link(‘Previous Entries’); ?></div>
    <div class=”alignright”><?php previous_posts_link(‘Next Entries’); ?></div>

    Had to swap the next_post_link and previous_post_link text to have them go in the right directions.. but in all this resolved fixed 3 days of hear tearing… cheers!

    t31os said: Well spotted, updated

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Post Type Archive Page – Pagination Isn't Working’ is closed to new replies.