Home > database >  In Wordpress display custom post type single page display previous and next links for custom categor
In Wordpress display custom post type single page display previous and next links for custom categor

Time:02-21

I have created a custom post type that has the ability to have custom taxonomies.

On the Single of the custom post type, I would like to be able to include "previous post" and "next post" buttons which display only if there are previous and next posts.

Set up for the custom post type in functions.php:

function the_custom_post_types() {

    $types = array(
        
        // Gallery
        array(
            'the_type'      => 'gallery', // becomes the archive url and used in archive template name (archive-{the_type}.php)
            'single'        => 'single',
            'plural'        => 'plural',
            'display'       => 'Gallery',
            'icon'          => 'dashicons-art',
            'show_in_rest'  => false,
            'supports'      => array( 'title', 'editor', 'custom-fields', 'post_tag', 'collection', 'thumbnail' ),
            'taxonomies'    => array( 'collection' ),
        ),

        // ... other CPTs ...
    );

    foreach ($types as $type) {
        $the_type   = $type['the_type'];
        $single     = $type['single'];
        $plural     = $type['plural']; 
        $display    = $type['display'];
        $icon       = $type['icon'];
        $gutenburg  = $type['show_in_rest'];
        $supports   = $type['supports'];
        $taxonomies = $type['taxonomies'];

        $labels = array(
            'name'                  => _x($display, 'post type general name'),
            'singular_name'         => _x($single, 'post type singular name'),
            'add_new'               => _x('Add New', $single),
            'add_new_item'          => __('Add New '. $single),
            'edit_item'             => __('Edit '.$single),
            'new_item'              => __('New '.$single),
            'view_item'             => __('View all '.$single),
            'search_items'          => __('Search all'.$plural),
            'not_found'.            => __('No '.$plural.' found'),
            'not_found_in_trash'    => __('No '.$plural.' found in Trash'),
            'parent_item_colon'     => ''
        );

        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'publicly_queryable'    => true,
            'show_ui'               => true,
            'query_var'             => true,
            'rewrite'               => true,
            'capability_type'       => 'post',
            'has_archive'           => true,
            'hierarchical'          => true,
            'show_in_rest'          => $gutenburg,
            'menu_position'         => 21,
            'supports'              => $supports,
            'menu_icon'             => $icon,
            'taxonomies'            => $taxonomies
        );
        
        register_post_type($the_type, $args);
        flush_rewrite_rules();
    }
}
add_action('init', 'the_custom_post_types');

And the taxonomy is created in a similar fashion like so:

function scaffolding_custom_taxonomies() {
    
    $types = array(
        
        array(
            'the_type'      => 'collection',
            'single'        => 'Collection',
            'plural'        => 'Collections',
            'display'       => 'Collections',
            'post_types'    => array( 'gallery' )
        )
    );
    
    foreach ($types as $type) {
        $the_type   = $type['the_type'];
        $single     = $type['single'];
        $plural     = $type['plural']; 
        $display    = $type['display'];
        $post_types = $type['post_types'];
        
        $labels = array(
            'name'              => _x( $display, 'taxonomy general name' ),
            'singular_name'     => _x( $single, 'taxonomy singular name' ),
            'search_items'      => __( 'Search ' . $plural ),
            'all_items'         => __( 'All ' . $plural ),
            'parent_item'       => __( 'Parent ' . $single ),
            'parent_item_colon' => __( 'Parent ' . $single . ':' ),
            'edit_item'         => __( 'Edit ' . $single ), 
            'update_item'       => __( 'Update ' . $single ),
            'add_new_item'      => __( 'Add New ' . $single ),
            'new_item_name'     => __( 'New ' . $single . ' Name' ),
            'menu_name'         => __( $plural ),
        );   
        
        $rewrite = array(
            'slug'              => $the_type
        );
        
        $args = array(
            'hierarchical'      => true,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => $rewrite
        );
        
        register_taxonomy( $the_type, $post_types, $args);
        flush_rewrite_rules();
    }
}

add_action( 'init', 'scaffolding_custom_taxonomies', 0 );

Currently, I have used the code below for the single page.

<nav >
    <div ><?php previous_post_link( '%link', 'previous' ); ?></div>
    <div ><?php next_post_link( '%link', 'next' ); ?></div>
</nav>

This cycles through all the posts in the CPT rather than just those in the current category.

If I include 'true' to the array like so:

<nav >
    <div ><?php previous_post_link( '%link', 'previous', true ); ?></div>
    <div ><?php next_post_link( '%link', 'next', true ); ?></div>
</nav>

It returns nothing.

How can I get the links to display and limit them to only those within the same custom category?

CodePudding user response:

The next and prev post link function includes a param for taxonomy, where the default is category. Since your taxonomy is named collection this should work.

/*
 * The parameters for next/previous post as defined in the function.
 * @param string       $format         Optional. Link anchor format. Default '&laquo; %link'.
 * @param string       $link           Optional. Link permalink format. Default '%title'.
 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term. Default false.
 * @param int[]|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
 */
 ?>
<nav >
    <div ><?php previous_post_link( '%link', 'previous', true, '', 'collection' ); ?></div>
<div ><?php next_post_link( '%link', 'next', true, '', 'collection' ); ?></div>
</nav>
  • Related