繁体   English   中英

在 Wordpress 显示自定义帖子类型 单页显示自定义类别的上一个和下一个链接

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

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

在自定义帖子类型的 Single 上,我希望能够包含“上一篇帖子”和“下一篇帖子”按钮,这些按钮仅在有上一篇和下一篇帖子时才会显示。

在 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 中的所有帖子,而不仅仅是当前类别中的帖子。

如果我像这样在数组中包含“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>

它什么都不返回。

我怎样才能获得要显示的链接并将它们限制为仅显示在同一自定义类别中的链接?

下一个和上一个帖子链接 function 包含一个分类参数,默认为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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM