Site logo

Archived topic

Next/Previous post links on CPT

21 replies · Started by wekhter on May 30, 2017

Viewing posts 16–22 of 22

No, I just deleted CSS: .nav-links { display: none; }

then tried your way, and the Next/previous are back again!

If you want to start a new topic where you can share your site privately using the Site URL i can take a look as to why unchecking Display post navigation isn't working.

I had 2 filters to customize the prev/next texts on Post, how to add another filter for CPT?

Many thanks

add_filter( 'next_post_link', function( $output, $format, $link, $post ) {
  	if ( ! $post ) {
	  return '';
	}
  
	return sprintf(
		'<div class="nav-next"><span class="next"><a href="%1$s" title="%2$s">Next Article</a></span></div>',
	  	get_permalink( $post ),
	  	$post->post_title
	);
}, 10, 4 );

add_filter( 'previous_post_link', function( $output, $format, $link, $post ) {
  	if ( ! $post ) {
	  return '';
	}
  
	return sprintf(
		'<div class="nav-previous"><span class="prev"><a href="%1$s" title="%2$s">Previous Article</a></span></div>',
	  	get_permalink( $post ),
	  	$post->post_title
	);
}, 10, 4 );

Hi there,

you can introduce more if condtions - for example:

add_filter( 'next_post_link', function( $output, $format, $link, $post ) {

  if ( ! $post ) {
    return '';

  } elseif ( is_singular( 'book' ) {
    // return your CPT nav link content here
  }
  // return standard post
  return sprintf(
  '<div class="nav-next"><span class="next"><a href="%1$s" title="%2$s">Next Article</a></span></div>',
    get_permalink( $post ),
    $post->post_title
  );
}, 10, 4 );

I tried it for custom post typle but it doesn't work.

add_filter( 'next_post_link', function( $output, $format, $link, $post ) {

  if ( ! $post ) {
    return '';

  } elseif ( is_singular( 'book' ) {
    // return your CPT nav link content here
  }
  // return standard post
  return sprintf(
  '<div class="nav-next"><span class="next"><a href="%1$s" title="%2$s">Next Article</a></span></div>',
    get_permalink( $post ),
    $post->post_title
  );
}, 10, 4 );

Hi Onalti,

could you raise a new topic explaining what you require as this topic is a mass of various requests.

This archived topic is closed to new replies.