Site logo

Archived topic

Prev/Next in CPT

13 replies · Started by Dave on March 9, 2019

Viewing posts 1–14 of 14

I’m trying to add previous and next links to a custom post type — I’ve checked out the threads below with no luck:
https://generatepress.com/forums/topic/nextprevious-post-links-on-cpt/
https://generatepress.com/forums/topic/adding-previousnext-links-to-custom-post-types/

I’ve created a CPT called ws_work and created a single-ws_work.php and content-ws_single.php file. Adding the code from the above threads into the functions.php hasn’t worked.

Am I missing something obvious?

Hi there,

Can you try this?:

add_action( 'generate_after_entry_content', function() {
    if ( is_singular( 'ws_work' ) ) : ?>
        <footer class="entry-meta">
            <nav id="nav-below" class="post-navigation">
                <span class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'generatepress' ); ?></span>
                <?php
                    previous_post_link( '<div class="nav-previous"><span class="prev" title="Previous">%link</span></div>', '%title' );
                    next_post_link( '<div class="nav-next"><span class="next" title="Next">%link</span></div>', '%title' );
                ?>
            </nav>
        </footer><!-- .entry-meta -->
    <?php endif;
} );

Let me know :)

Hi Tom,

Thanks for getting back to me...on a Saturday, too. :)

I’m afraid there’s no luck there, either.

Here’s the functions.php code I’m using to generate the CPT...if this helps:

function create_post_type() {
  register_post_type( 'ws_work',
    array(
      'labels' => array(
        'name' => __( 'Work' ),
        'singular_name' => __( 'Work' )
      ),
      'public' => true,
      'has_archive' => true,
	  'rewrite' => array('slug' => 'project'),
	  'supports' => array('title','thumbnail','editor','revisions','page-attributes','excerpt'),
	  'show_in_rest' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

If it’s helpful, I’m happy to provide admin credentials, too.

Thanks again for your help with this,

Dave

Hi there,

Strange, it's like those two core functions aren't working at all.

Just to confirm, other published posts exist in that post type, yea?

Hi Tom,

Yes, that’s correct — it’s really odd.

I could set up an admin account and/or FTP if that would be helpful? The admin editor is active so it’s possible to view/edit the files directly from the dashboard.

Dave

What happens if you replace:

previous_post_link( '<div class="nav-previous"><span class="prev" title="Previous">%link</span></div>', '%title' );

With:

$prev = previous_post_link( '<div class="nav-previous"><span class="prev" title="Previous">%link</span></div>', '%title' );
var_dump($prev);

Does it output anything on the page?

Also, I'm seeing the code twice in the HTML. Make sure you only have the function added once.

Hi Tom,

Missed this notification, sorry for the delay.

That returns NULL.

Yes, I noticed that about the HTML, too. The code is only added once in functions.php and isn’t duplicated in any of the template files or in a GeneratePress element/hook. Weird behaviour.

Dave

Hmm, from what I know, it would only return null if no other posts exist.

Does it still happen if you deactivate your other plugins/custom functions?

Hi Tom,

Yes, I’ve just deactivated everything except for GP and the issue remains (returns NULL).

Very odd...

Dave

I just gave this a shot with your CPT code and the code I shared above, and here's what I get: http://prntscr.com/my123t

So we've ruled out other plugins causing it not to work. What about if you disable any other custom functions?

Hi Tom,

I’ve adjusted the functions.php code to this:

<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    function chld_thm_cfg_locale_css( $uri ){
        if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
            $uri = get_template_directory_uri() . '/rtl.css';
        return $uri;
    }
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );

if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
    function chld_thm_cfg_parent_css() {
        wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array(  ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );

// END ENQUEUE PARENT ACTION

/* Work Custom Post Type */
function create_post_type() {
  register_post_type( 'ws_work',
    array(
      'labels' => array(
        'name' => __( 'Work' ),
        'singular_name' => __( 'Work' )
      ),
      'public' => true,
      'has_archive' => true,
	  'rewrite' => array('slug' => 'project'),
	  'supports' => array('title','thumbnail','editor','revisions','page-attributes','excerpt'),
	  'show_in_rest' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

add_action( 'generate_after_entry_content', function() {
    if ( is_singular( 'ws_work' ) ) : ?>
        <footer class="entry-meta">
            <nav id="nav-below" class="post-navigation">
                <span class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'generatepress' ); ?></span>
                <?php
                    $prev = previous_post_link( '<div class="nav-previous"><span class="prev" title="Previous">%link</span></div>', '%title' );
					var_dump($prev);
                    next_post_link( '<div class="nav-next"><span class="next" title="Next">%link</span></div>', '%title' );
                ?>
            </nav>
        </footer><!-- .entry-meta -->
    <?php endif;
} );

And it’s producing the same NULL result.

Dave

I'm afraid I'm stumped. That exact code works on my installs (as my screenshot showed).

Do you have another test site you can test it on?

No problem — it’s not the end of the world. :)

I’ll put this on the backburner for now.

Thanks for all of your help. GeneratePress is wicked, so light and fast! :)

Dave

Sounds good - it's definitely worth testing on another install just to see if it's something to do with that site.

Glad you're enjoying it!

This archived topic is closed to new replies.