自定义帖子类型永久链接,具有自定义分类法和重复的帖子名称和Slugs



我正在尝试创建不同的社区,每个社区都有自己的一组页面(关于,服务等)。我目前设置的永久链接结构是sitename.com/community/state/city/parent-page-name/城市和州是从位置自定义分类法中提取的,其中城市是州下的子节点。如果我想为堪萨斯州托皮卡的一个名为The Oaks的社区创建一个about页面,那么url将是:sitename.com/community/ks/topeka/the-oaks/about然而,当我尝试用许多不同的about页面创建多个社区时,对于第一个发布的社区,所有的about页面都默认为相同的about页面。

我的自定义帖子类型是这样的:

function community_post_type() {
$singular = 'Community';
$plural = 'Communities';
register_post_type('community',
array(
'labels' => array(
'name' => __($plural, 'base'),
'singular_name' => __($singular, 'base'),
'all_items' => __('All ' . $plural, 'base'),
'add_new' => __('Add New ' . $singular, 'base'),
'add_new_item' => __('Add New ' . $singular, 'base'),
'edit' => __('Edit ' . $singular, 'base'),
'edit_item' => __('Edit ' . $singular, 'base'),
'new_item' => __('New ' . $singular, 'base'),
'view_item' => __('View ' . $singular, 'base'),
'search_items' => __('Search ' . $plural, 'base'),
'not_found' => __('Nothing found in the Database.', 'base'),
'not_found_in_trash' => __('Nothing found in Trash', 'base'),
'parent_item_colon' => 'Main Community',
),
'hierarchical' => true,
'description' => __('This is the ' . $singular . ' post type', 'base'),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'menu_icon' => 'dashicons-location-alt',
'has_archive' => true,        
'capability_type' => 'page',
'supports' => array('title', 'editor', 'revisions', 'thumbnail', 'excerpt', 'page-attributes', 'custom-fields'),
'show_in_rest' => true,
'menu_position' =>  4,
'taxonomies'  => array( 'location' ),
'rewrite' => array('slug' => 'community/%comm%'),
)
);
}

下面是我修改永久链接的代码:

function change_permalinks( $post_link, $id = 0){
$post = get_post($id);  
if ( is_object( $post ) && $post->post_type == 'community' ){
$terms = wp_get_object_terms( $post->ID, 'location' );
if( $terms ){
return str_replace( '%comm%' , $terms[0]->slug .'/'. $terms[1]->slug , $post_link );
}
}
return $post_link;  
}
add_filter( 'post_type_link', 'change_permalinks', 1, 3 );
function generated_rewrite_rules() {
add_rewrite_rule(
'^community/(.*)/(.*)/(.*)/?$',
'index.php?post_type=community&name=$matches[3]',
'top'
);
}
add_action( 'init', 'generated_rewrite_rules' );

如果我使用about1, about2, about3等作为页面标签,那么它可以正常工作,但我不能使用相同的标签,否则它会在700个位置中的每个位置进入相同的页面。

您可以使用这个可用的查询变量列表来调整您的url以接收所需的任何帖子

https://codex.wordpress.org/WordPress_Query_Vars

我想对于你的情况,它可能看起来像这样,我没有测试过但这是一个提示,你可以使用

function generated_rewrite_rules() {
add_rewrite_rule(
'^community/(.*)/(.*)/(.*)/?$',
'index.php?post_type=community&taxonomy=location&term=$matches[1]&name=$matches[3]',
'top'
);
}
add_action( 'init', 'generated_rewrite_rules' );

这将告诉wordpress从第一个匹配中获取具有分类法位置和特定状态的帖子。

你当然可以调整它来更准确地匹配你的结果,或者在你的post模板文件中使用那些query_vars来更正确地构建get_post/WP_Query的参数,可以这么说,这里可以找到一个如何看起来的例子

https://developer.wordpress.org/reference/classes/wp_query/taxonomy-parameters

我还建议,如果它不像预期的那样工作,钩子到pre_get_posts中打印出查询并检查它的样子,以便进一步调试

的例子:

function check_query_vars( $query ) {
if ( ! is_admin()) {
// Not a query for an admin page.
//print the queried variables or if you like to see EVERYTHING dump $query
print_r($query->query_vars);

}
}
add_action( 'pre_get_posts', 'check_query_vars' );

最新更新