php - 带有自定义永久链接和标签的 WordPress 自定义帖子类型
问题描述
我在这里找到了一些示例来编写代码。但现在我被困住了,找不到任何解决这个问题的方法:
我创建了一个自定义帖子类型和一个设置页面,用户可以在其中为此 CPT 设置自己的永久链接结构。比如用户可以设置分类、帖子id、帖子名称等等……下面是第一次运行的截图:
问题是,如果帖子名称在 URL 中但没有 ID,我只能访问前端的页面。我是否使用%post_id%或%postname% als 占位符都没关系。
- 这有效:https ://example.org/swipefile/test/
- 这不是:https ://example.org/swipefile/5/ (帖子 ID 为 5)
但它也应该与其他永久链接标签/占位符一起使用。
这是代码......在https://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2的帮助下
我也测试过 add $wp_rewrite->flush_rules();
,但没有成功。
add_action( 'init', 'init_landingpage_cpt' );
function init_landingpage_cpt() {
$settings = new LP_Admin_Settings();
$post_type = 'landingpage';
$slug = $settings->get( 'slug' ); // Value: swipefile/%postname%
register_post_type( $post_type, array(
'description' => esc_html__( 'Library with landingpages.', 'lp_txt' ),
'public' => true,
'publicly_queryable' => true,
'query_var' => true,
'show_ui' => true,
'show_in_menu' => 'landingapge-lib',
'rewrite' => array(
'slug' => $slug,
'with_front' => false,
),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'labels' => array(
'name' => esc_html__( 'Landingpages', 'lp_txt' ),
'singular_name' => esc_html__( 'Landingpage', 'lp_txt' ),
'menu_name' => esc_html__( 'Landingpages', 'lp_txt' ),
'name_admin_bar' => esc_html__( 'Landingpages', 'lp_txt' ),
'add_new' => esc_html__( 'Add new', 'lp_txt' ),
'add_new_item' => esc_html__( 'Add new landingpage', 'lp_txt' ),
'new_item' => esc_html__( 'New landingpage', 'lp_txt' ),
'edit_item' => esc_html__( 'Edit landingpage', 'lp_txt' ),
'view_item' => esc_html__( 'View landingpage', 'lp_txt' ),
'all_items' => esc_html__( 'Landingpages', 'lp_txt' ),
'search_items' => esc_html__( 'Search landingpages', 'lp_txt' ),
'parent_item_colon' => esc_html__( 'Parent landingpages:', 'lp_txt' ),
'not_found' => esc_html__( 'No landingpages found.', 'lp_txt' ),
'not_found_in_trash' => esc_html__( 'No landingpages found in trash.', 'lp_txt' ),
),
) );
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( "%post_id%", '([^/]+)', "{$post_type}=" );
$wp_rewrite->add_rewrite_tag( "%postname%", '([^/]+)', "{$post_type}=" );
$wp_rewrite->add_permastruct( $post_type, "/{$slug}/", false );
}
add_filter( 'post_type_link', 'post_type_link_landingpage', 10, 3 );
function post_type_link_landingpage( $permalink, $post_id, $leavename ) {
$post = get_post( $post_id );
if ( ! empty( $post ) && ! empty( $permalink ) && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$search = array(
$leavename ? '' : '%postname%',
'%post_id%',
);
$replace = array(
$post->post_name,
$post->ID,
);
$permalink = str_replace( $search, $replace, $permalink );
}
return $permalink;
}
解决方案
推荐阅读
- json - 快速解析 JSON 数据
- python - 如何替换熊猫中的数据框
- html - Tumblr 标题和提要间距
- python - PYTHON 中的错误:TypeError:ufunc 'multiply' 不包含签名匹配类型 dtype('
我在 python 中运行这段代码
with open("test.txt", "r") as networkFile: for line in networkFile: src = line.split(
- node.js - SailsJS - 永远加载路线
- java - 以java语言导入父“UpTitle”类
- php - 在 Woocommerce 中隐藏只有 1 个变量的下拉菜单
- python - 分组熊猫系列的堆积条形图
- javascript - 无法使用 Node.js 将表单数据发送到 MongoDB 数据库
- r - R中的日期和时间减少