0

I have a website that is built almost entirely on the page.php template using Advanced custom fields flexible content field to determine what gets displayed. As part of that flexible content I have a reference field that fetches custom post types and displays a list of them.

This reference field's template looks like this and it sits inside the main WordPress loop.

    <?php 

  $margin = get_sub_field('remove_top_and_bottom_margin');

  if ($margin) {

    foreach($margin as $result) {

      if ($result == 'bottom') {
        $no_bottom = 'no-bottom-margin';
      }

      if ($result == 'top') {
        $no_top = 'no-top-margin';
      } 
    }

  }

?>

<?php 

// Here we choose the post type to reference. This post type 
// also has a flexible layout (see the get_template_part('templates/content', 'flexible'). 

$reference = get_sub_field('reference'); 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(

    "post_type" => $reference,
    "posts_per_page" => 1,
    "paged" => $paged

);

echo '<pre>';
var_dump($paged);
echo '</pre>';

$inner_query = new WP_Query($args); 

if ( $inner_query->have_posts() ) : ?>

  <div class="reference <?php echo $no_top . ' ' . $no_bottom; ?>">

    <?php   while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>

      <div class="reference-entity">

            <div class="entry-title">
          <h3><?php the_title(); ?></h3>
            <?php get_template_part('templates/entry-meta'); ?>
        </div>

            <?php get_template_part('templates/content', 'flexible');

            ?>

      </div>

    <?php endwhile; 

    if ($inner_query->max_num_pages > 1) :  // check if the max number of pages is greater than 1  ?>

      <nav class="prev-next-posts">
        <div class="prev-posts-link">
          <?php echo get_next_posts_link( 'Older Entries', $inner_query->max_num_pages ); // display older posts link ?>
        </div>
        <div class="next-posts-link">
          <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
        </div>
      </nav>

    <?php endif; 

    wp_reset_postdata(); ?>

  </div>

<?php else: 

  echo 'No posts found. Try adding one.';

endif; 

When I click the pagination link to go back I get a 404 Error and the URL has a /page/2 in it. Any help?

  • 1
    Your code should work unless it is a static front page – Pieter Goosen Aug 12 '15 at 6:03
0

I have figured out the problem. There was a naming clash between my post type insights and the page name (also insights) which was confusing WordPress. Changing the post type to the singular insight fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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