[Support request] Prev/Next in CPT

Home Forums Support [Support request] Prev/Next in CPT

Home Forums Support Prev/Next in CPT

  • This topic has 13 replies, 2 voices, and was last updated 4 years ago by Tom.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #833572
    Dave

    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?

    GeneratePress 2.2.2
    GP Premium 1.7.8
    #833994
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #834036
    Dave

    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

    #834197
    Tom
    Lead Developer
    Lead Developer

    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?

    #834306
    Dave

    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

    #834743
    Tom
    Lead Developer
    Lead Developer

    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.

    #837309
    Dave

    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

    #837878
    Tom
    Lead Developer
    Lead Developer

    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?

    #838407
    Dave

    Hi Tom,

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

    Very odd…

    Dave

    #839239
    Tom
    Lead Developer
    Lead Developer

    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?

    #839381
    Dave

    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

    #840048
    Tom
    Lead Developer
    Lead Developer

    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?

    #841931
    Dave

    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

    #842335
    Tom
    Lead Developer
    Lead Developer

    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!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.