Ethos post prev/next navigation arrows on custom post type posts?

Is there a way to add the previous/nest post arrows to a custom post type post? I’ve added the <?php x_entry_navigation(); ?> code to my custom post type single posts, but it’s not showing on the front end.

I’m using the following code in my child theme functions.php file to customize the arrows and move them to the bottom of the post page, but I can’t figure out how to get them to work on my custom post type posts.

function x_entry_navigation() {

  $stack = x_get_stack();
  if ( $stack == 'ethos' ) {

    $left_icon  = '<i class="x-icon-chevron-left" data-x-icon="&#xf053;"></i> PREV';
    $right_icon = 'NEXT <i class="x-icon-chevron-right" data-x-icon="&#xf054;"></i>';
  } else {
    $left_icon  = '<i class="x-icon-arrow-left" data-x-icon="&#xf060;"></i>';
    $right_icon = '<i class="x-icon-arrow-right" data-x-icon="&#xf061;"></i>';
  }

  $is_ltr    = ! is_rtl();
  $prev_post = get_adjacent_post( true, '', false );
  $next_post = get_adjacent_post( true, '', true );
  $prev_icon = ( $is_ltr ) ? $left_icon : $right_icon;
  $next_icon = ( $is_ltr ) ? $right_icon : $left_icon;
  ?>

  <div class="x-nav-articles">
    <?php if ( $prev_post ) : ?>
      <a href="<?php echo get_permalink( $prev_post ); ?>" title="<?php __( 'Previous Post', '__x__' ); ?>" class="prev">
        <?php echo $prev_icon; ?>
      </a>
    <?php endif; ?>

    <?php if ( $next_post ) : ?>
      <a href="<?php echo get_permalink( $next_post ); ?>" title="<?php __( 'Next Post', '__x__' ); ?>" class="next">
        <?php echo $next_icon; ?>
      </a>
    <?php endif; ?>
  </div>

  <?php
}
add_action( 'x_after_the_content_end', 'x_print_nav', 999 );
function x_print_nav(){
	if(is_singular( 'post' )){
		x_entry_navigation();
	}
}

The div is added in the correct place, but it’s empty with no nav arrows. Is there a way to revise this code to work on custom post types?

Hello @designerkev,

Thanks for writing in!

The PHP code that you added will only be applied to single blog posts.

add_action( 'x_after_the_content_end', 'x_print_nav', 999 );
function x_print_nav(){
	if(is_singular( 'post' )){
		x_entry_navigation();
	}
}

If you want to include custom post types, you will have to update the code above and use this:

add_action( 'x_after_the_content_end', 'x_print_nav', 999 );
function x_print_nav(){
	if( is_singular() ){
		x_entry_navigation();
	}
}

By using is_singular() in the code will make sure that the entry navigation will display in all custom post types.

Hope this helps.

Thank you for the reply, but this didn’t work. Still behaving the exact same.

Hello @designerkev,

Could you please provide us a url of your single post and your single custom post type? We would love to check your site settings and your child theme as well. Please create a secure note with the following info:
– Link to your site
– WordPress Admin username / password

To know how to create a secure note, please check this out: https://theme.co/apex/forum/t/how-to-get-support/288

Thank you in advance.

Hi @designerkev,

It’s working, but the issue is Wordpress’ own function get_adjacent_post() is not working for custom post type. It would require customization such as this https://stackoverflow.com/questions/10376891/make-get-adjacent-post-work-across-custom-post-types.

Example, let’s say you implemented the same code from that link then you would need to change these two lines

  $prev_post = get_adjacent_post( true, '', false );
  $next_post = get_adjacent_post( true, '', true );

with this

  $prev_post = mod_get_adjacent_post('prev', array('post', 'evidence'));
  $next_post = mod_get_adjacent_post('next', array('post', 'evidence'));

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.