1

This question already has an answer here:

I have built a news section in my Wordpress site with the advanced custom fields and custom post type ui plugins. On the custom news overview page (not the standard wordpress posts) I wanted to limit the number of news per page to six and provide a navigation with paginate_links. I've built the following snippet:

<section class="news-main" role="main">
    <?php
        $args = array(
            'post_type' => 'news',
            'posts_per_page' => '6'
        );
        $the_query = new WP_Query( $args );
        $temp_query = $wp_query;
        $wp_query = NULL;
        $wp_query = $the_query;
        $pagination = array(
            'base' => '%_%',
            'format' => '?page=%#%',
            'total' => $the_query->max_num_pages,
            'prev_next' => True,
            'prev_text' => __( '<< Previous' ),
            'next_text' => __( 'Next >>' )
        );
        if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <article class="news-snippet">
            <header class="news-box">
                <h2 class="mus-hi slogan-head fancy"><span class="fancy-hook"><?php the_time( 'j. F Y' ); ?></span></h2>
                <a href="<?php the_permalink(); ?>"><p class="bran-hn slogan-state closure"><?php the_title(); ?></p></a>
            </header>
            <div class="news-wrap">
                <p class="news-excerpt"><?php echo acf_excerpt( 'news_post', 35, ' <span class="news-more-inbox">[...]</span>' ); ?></p>
                <p class="bran-hn news-more"><a href="<?php the_permalink(); ?>">More &rarr;</a></p>
            </div>
        </article>
    <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else: ?>
        <p>No entries</p>
    <?php endif; ?>
    <nav>
        <?php echo paginate_links( $pagination );
                $wp_query = NULL;
                $wp_query = $temp_query; ?>
    </nav>
</section>

First the arguments for the wp_query are defined, then $the_query is set with a WP_Query afterwards the $wp_query variable is saved into a temporary variable, reset with NULL and then set to the $the_query variable. In the last step the settings array for the paginate_links is set. Then there is the loop for outputting news posts. After the loop the paginate_links function is called and finally the $wp_query got reset and written back the content of the temporary $temp_query variable.

If I have let's say 15 blog posts my output looks like the following:

paginate_links layout

If I hover over the number three, the end of the slug looks like the following:

slug for page 3

Problem is now the previous and next links are not shown even that they are set. While the main problem is the given slug - if I click the number three I land on a 404 page. "page 3" where I get directed to is unknown for wordpress. Thanks in advance Ralf

Update:

$args = array(
    'post_type' => 'news',
    'posts_per_page' => '1'
);  
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $current_page = new WP_Query('post_type=news&posts_per_page=1&paged=' . $paged);
    $pagination = array(
        'base' => '%_%',
        'format' => '?paged=%#%',
        'mid-size' => 1,
        'current' => $current_page,
        'total' => $total_pages,
        'prev_next' => True,
        'prev_text' => __( '<< Previous' ),
        'next_text' => __( 'Next >>' )
    );
}

Update 2:

Ok with the following changes to the first part of the code i got the nav working

$args = array(
    'post_type' => 'news',
    'posts_per_page' => '3',
    'paged' => get_query_var( 'paged' )
);  
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1) {
    $the_paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $pagination = array(
        'base' => @add_query_arg('paged','%#%'),
        'format' => '?paged=%#%',
        'mid-size' => 1,
        'current' => $the_paged,
        'total' => $total_pages,
        'prev_next' => True,
        'prev_text' => __( '<< Previous' ),
        'next_text' => __( 'Next >>' )
    );
}

The only question i have left (the only thing which is still misbehaving) is it possible that the first link has link instead of link/?paged=1

marked as duplicate by Chip Bennett, fuxia Apr 7 '14 at 12:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Why are you using h2 tag for date span? – ahmetlutfu Apr 5 '14 at 22:46
4

Regarding the number of pages displayed, read through the arguments for paginate_links, specifically end_size and mid_size.

As for the 404- The problem is there is no page 3. Whether or not a page returns content or loads the 404 template is based on the results of the main query, not your custom query you run in the template. If this is a page post type, try setting the paged query var instead of page.

  • Thanks, yeah the numbering is working fine now, needed some refinement in the settings, as well as the previous and next link. So basically the pagination looks fine already only it isn't functional at all. If i click now the two i get to ?paged=2 and no more 404 template. But on the downside still the articles from the initial page are shown and number one sticks to the current class instead of the paged 2 i am on. Also the Previous link isn't showing. – rpk Apr 5 '14 at 22:29
  • And about your suspicion that everything relies onto the main query instead of the custom query i run - i thought i wit $wp_query = $the_query; i 've "transfered" my custom query over to the main query and made it accessible to the paginate_links function. – rpk Apr 5 '14 at 22:31
  • I've added an update to the initial post with an updated code snippet for the paginate_links relating php parts (tried to include paged - but still not working :/ ). – rpk Apr 5 '14 at 22:33
  • 1
    whether or not a 404 is returned happens before your template is loaded, that code never runs if the query vars aren't correct. and you're seeing the first 6 posts on every page because you don't set paged in your custom query args. – Milo Apr 5 '14 at 22:33
  • ahhhhhhhhhhhh i see. i was a bit slow - to use paged already in the custom query i haven't understood and realized, but makes perfect sense. silly me :( Ok the display of articles works now perfectly. The only thing left to fix is that the paginate_links nav is reflecting the changes. But i will have to play and try around tomorrow morning. Now is already late. Thanks! – rpk Apr 5 '14 at 22:55

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