我在尝试获取自定义post类型permalink时遇到分页不起作用的问题。我在这里自定义了帖子类型“songs”和自定义分类“singer”:
example.com/songs/
// 自定义帖子类型
example.com/singer/
// 自定义分类
example.com/songs/article
// 后Permalink
我想让它看起来像
example.com/singer/article
/// 地点com/自定义分类法/post
这是我的自定义邮件类型代码
add_action( \'init\', \'your_prefix_register_post_type\' );
function your_prefix_register_post_type() {
$labels = [
\'name\' => esc_html__( \'Songs\', \'your-textdomain\' ),
\'singular_name\' => esc_html__( \'Song\', \'your-textdomain\' ),
\'add_new\' => esc_html__( \'Add New\', \'your-textdomain\' ),
\'add_new_item\' => esc_html__( \'Add new song\', \'your-textdomain\' ),
\'edit_item\' => esc_html__( \'Edit Song\', \'your-textdomain\' ),
\'new_item\' => esc_html__( \'New Song\', \'your-textdomain\' ),
\'view_item\' => esc_html__( \'View Song\', \'your-textdomain\' ),
\'view_items\' => esc_html__( \'View Songs\', \'your-textdomain\' ),
\'search_items\' => esc_html__( \'Search Songs\', \'your-textdomain\' ),
\'not_found\' => esc_html__( \'No songs found\', \'your-textdomain\' ),
\'not_found_in_trash\' => esc_html__( \'No songs found in Trash\', \'your-textdomain\' ),
\'parent_item_colon\' => esc_html__( \'Parent Song:\', \'your-textdomain\' ),
\'all_items\' => esc_html__( \'All Songs\', \'your-textdomain\' ),
\'archives\' => esc_html__( \'Song Archives\', \'your-textdomain\' ),
\'attributes\' => esc_html__( \'Song Attributes\', \'your-textdomain\' ),
\'insert_into_item\' => esc_html__( \'Insert into song\', \'your-textdomain\' ),
\'uploaded_to_this_item\' => esc_html__( \'Uploaded to this song\', \'your-textdomain\' ),
\'featured_image\' => esc_html__( \'Featured image\', \'your-textdomain\' ),
\'set_featured_image\' => esc_html__( \'Set featured image\', \'your-textdomain\' ),
\'remove_featured_image\' => esc_html__( \'Remove featured image\', \'your-textdomain\' ),
\'use_featured_image\' => esc_html__( \'Use as featured image\', \'your-textdomain\' ),
\'menu_name\' => esc_html__( \'Songs\', \'your-textdomain\' ),
\'filter_items_list\' => esc_html__( \'Filter songs list\', \'your-textdomain\' ),
\'filter_by_date\' => esc_html__( \'\', \'your-textdomain\' ),
\'items_list_navigation\' => esc_html__( \'Songs list navigation\', \'your-textdomain\' ),
\'items_list\' => esc_html__( \'Songs list\', \'your-textdomain\' ),
\'item_published\' => esc_html__( \'Song published\', \'your-textdomain\' ),
\'item_published_privately\' => esc_html__( \'Song published privately\', \'your-textdomain\' ),
\'item_reverted_to_draft\' => esc_html__( \'Song reverted to draft\', \'your-textdomain\' ),
\'item_scheduled\' => esc_html__( \'Song scheduled\', \'your-textdomain\' ),
\'item_updated\' => esc_html__( \'Song updated\', \'your-textdomain\' ),
];
$args = [
\'label\' => esc_html__( \'Songs\', \'your-textdomain\' ),
\'labels\' => $labels,
\'description\' => \'\',
\'public\' => true,
\'hierarchical\' => false,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'show_in_rest\' => true,
\'query_var\' => true,
\'can_export\' => true,
\'delete_with_user\' => true,
\'has_archive\' => true,
\'rest_base\' => \'\',
\'show_in_menu\' => true,
\'menu_position\' => \'\',
\'menu_icon\' => \'dashicons-format-audio\',
\'capability_type\' => \'post\',
\'supports\' => [\'title\', \'editor\', \'thumbnail\'],
\'taxonomies\' => [\'singer\', \'rapper\', \'category\', \'movie\', \'album\', \'composer\', \'lyricist\', \'by-year\', \'mood\', \'file-link\', \'post-id\', \'yt_id\', \'artist\'],
\'rewrite\' => array(\'slug\' => \'songs\'),
];
register_post_type( \'songs\', $args );
}
和用于为post permalink重写slug的代码
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, \'singer\' );
if( $terms ){
return str_replace( \'/songs/\' , \'/\' . $terms[0]->slug . \'/\' , $post_link );
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'wpa_course_post_link\', 1, 3 );
function custom_rewrite_rules() {
add_rewrite_rule(
\'^(.*)/(.*)/?$\',
\'index.php?post_type=songs&name=$matches[2]\',
\'top\'
);
}
add_action( \'init\', \'custom_rewrite_rules\' );
感谢您的帮助。