在 Wordpress 显示自定义 post 中键入单页显示自定义类别的上一个和下一个链接

In Wordpress display custom post type single page display previous and next links for custom category

我已经创建了一个自定义 post 类型,它能够拥有自定义分类法。

在自定义 post 类型的 Single 上,我希望能够包含“上一个 post”和“下一个 post”按钮,这些按钮仅在有上一个和下一个 posts.

设置自定义 post 输入 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');

分类法的创建方式与此类似:

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 );

目前单页我使用了下面的代码

<nav class="artwork__pagination">
    <div class="artwork__prev"><?php previous_post_link( '%link', 'previous' ); ?></div>
    <div class="artwork__next"><?php next_post_link( '%link', 'next' ); ?></div>
</nav>

这将循环遍历 CPT 中的所有 post,而不仅仅是当前类别中的那些。

如果我像这样将 'true' 包含到数组中:

<nav class="artwork__pagination">
    <div class="artwork__prev"><?php previous_post_link( '%link', 'previous', true ); ?></div>
    <div class="artwork__next"><?php next_post_link( '%link', 'next', true ); ?></div>
</nav>

它returns没什么。

如何获取要显示的链接并将它们限制为仅显示在同一自定义类别中的链接?

下一个和上一个 post link 函数包含一个分类参数,默认值为 category。由于您的分类法被命名为 collection 这应该有效。

/*
 * 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 class="artwork__pagination">
    <div class="artwork__prev"><?php previous_post_link( '%link', 'previous', true, '', 'collection' ); ?></div>
<div class="artwork__next"><?php next_post_link( '%link', 'next', true, '', 'collection' ); ?></div>
</nav>