[英]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 '« %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.