0

I'm using custom post types and custom fields plugins for my custom post types.

I have all other paginations working smoothly on my website except for my single.php. I have a custom post type of "Products" with dozens of products assigned to a custom taxonomy of "Types" and so far I've gotten all of the pagination on my other pages to work with custom queries.

However, in single.php, I dont need to use custom queries so I'm not sure if Im going about this the right way.

Here is my very simple code:

<div class="wrap">
<?php while ( have_posts() ) : the_post(); ?>

//my custom post type content
<?php the_field('image'); ?>
<?php the_content(); ?>

        <?php endwhile; // end of the loop. ?>

<nav>
<?php previous_post_link('&laquo; Prev') ?>
<?php next_post_link('Next &raquo;') ?>
</nav>

</div><!--endwrap-->

When I add the next and previous post links, I can see the Next and Prev link and the bottom of my page, but it is NOT a link to the next or prev page. Its just plain ol static text that does nothing.

Im not using custom queries on this one because like I said you dont have to for single.php to display your custom post type content.

Any suggestions?

  • Have you added the 'paged' and 'posts_per_page' parameters in the WP_Query()? Could you add that code too in your question. – WisdmLabs Nov 10 '14 at 11:17
  • 1
    im not using a custom query tho, just a regular loop. single.php doesn't need a custom query to display custom post types – Lucas Santos Nov 10 '14 at 11:18
2

You will need to add %link to the first parameter ($format) of next_post_link and previous_post_link

This will generate the html links to the respective posts

EDIT 1

Just on your statement about custom queries, did you had a look at how pre_get_posts can be used to alter the main query as needed. It is never a good idea replacing the main query with a custom query.

Here is an example on adding your custom post type to your homepage

function include_post_type( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'my_post_type') );
    }
}
add_action( 'pre_get_posts', 'include_post_type' );

EDIT 2

Just on your comment. The posts are displayed according to post date, either ASC or DESC depending on your blog settings.

You can set both links to only page to the next/previous post within the same term. Have a look at the third parameter ($in_same_term) and fifth parameter ($taxonomy)

By default $in_same_term is set to false and $taxonomy is set to category. You can set this accordingly.

Example:

next_post_link( '%link', 'Next post in types', TRUE, ' ', 'types' ); 

This will page to the next post within the same term that this particular post belongs to within the types taxonomy

  • Check my update. On your code above, if it works as expected, I would not mess with it right now. Check this post I've a while ago. You can play around with the code. – Pieter Goosen Nov 10 '14 at 12:10
  • Hey thanks for all the help Pieter really appreciate it, im going to bed but i'll talk to you through chat later on and show you my taxonomy.php code for my products if you'd like. – Lucas Santos Nov 10 '14 at 13:26
  • Let us continue this discussion in chat. – Pieter Goosen Nov 10 '14 at 13:43
1

You could also create a template tag like what the Twenty Fourteen default theme includes rather than hard code it into the single file.

if ( ! function_exists( 'twentyfourteen_post_nav' ) ) :

function twentyfourteen_post_nav() {
    // Don't print empty markup if there's nowhere to navigate.
    $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
    $next     = get_adjacent_post( false, '', false );

    if ( ! $next && ! $previous ) {
        return;
    }

    ?>
    <nav class="navigation post-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentyfourteen' ); ?></h1>
        <div class="nav-links">
            <?php
            if ( is_attachment() ) :
                previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'twentyfourteen' ) );
            else :
                previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ) );
                next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ) );
            endif;
            ?>
        </div><!-- .nav-links -->
    </nav><!-- .navigation -->
    <?php
}
endif;

And then add the tag before the endwhile

      // Previous/next post navigation.
    twentyfourteen_post_nav();
                    }
    endwhile; ?>

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.