在Wordpress中,我有三种自定义帖子类型:展览、书籍、电影。我有一个具有两个分类术语(store-1和store-2)的分类法,可以应用于所有CPT。
我需要实现的是有一个永久链接www.mysite.com/exhibitions的页面,其中列出了标记为store-1和store-2的帖子,这可以使用CPT轻松完成。然而,我也希望有永久链接,如www.mysite.com/exhibitions/store-1,其中列出了来自展览CPT的商店分类的所有帖子(每个CPT和每个分类术语依此类推)。然而,这在固定链接中创建了CPT和分类术语的混合,而我不知道如何做到这一点。
我的猜测是颠倒逻辑,创建三个自定义分类,分别命名为exhibitions、book和movies (当然,我需要将CPT slug更改为cpt-exhibitions、cpt-book和cpt-movies),并为其中的每一个创建术语store-1和store-2。通过这种方式,我会得到像www.mysite.com/exhibitions/store-1这样的永久链接。关于固定链接,比如www.mysite.com/exhibitions,我正在考虑创建一个带有插件“www.mysite.com/exhibitions”的页面,以及该页面的自定义模板。在模板的php中,我会添加一个循环来获取所有的cpt-exhibitions帖子,而不管分类术语是什么。
这不是一个真正干净的解决方案,因为我基本上会在后端有三个空页面,仅用于固定链接目的。所以我想知道:是否有一个特定的函数或重写规则来实现我需要的CPT,命名为展览,书籍,电影和只有一个自定义分类(例如商店)应用于所有CPT?
感谢你的见解!
发布于 2021-03-15 01:16:57
我曾经有一个这样的设置,并用一些重写规则解决了它。查看这段代码,它确实提供了您需要的固定链接,但可能需要在您的端进行一些测试。是的,不要害怕在WordPress中重写规则。从字面上看,没有什么是你不能用它们做的,尽管有些事情可能是有代价的(即性能问题)
add_filter('init', 'create_movies_taxonomies');
add_filter('init', 'create_movies_cpt');
add_action('init', 'movies_rewrite_rules');
add_filter('post_type_link', 'movies_post_permalinks', 10, 2);
/**
* First create Stores taxonomy
*/
function create_movies_taxonomies() {
register_taxonomy('stores', ['movies'],
[
'label' => __('Stores', 'textdomain'),
'public' => true,
'show_in_nav_menus' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_tagcloud' => false,
'show_in_rest' => true,
'hierarchical' => false,
'rewrite' => ['slug' => 'store', 'hierarchical' => false, 'with_front' => false],
'show_admin_column' => true,
'query_var' => true
]
);
}
/**
* Then create Movies CPT with support for Stores and proper rewrites
*/
function create_movies_cpt() {
register_post_type('movies',
[
'label' => __('Movies', 'textdomain'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'map_meta_cap' => true,
'show_in_rest' => true,
'hierarchical' => false,
'rewrite' => [
'slug' => '/movies/%stores%',
'with_front' => false,
'feeds' => false
],
'query_var' => true,
'supports' => ['title', 'tags', 'editor', 'comments', 'custom-fields', 'thumbnail'],
'has_archive' => 'movies',
'taxonomies' => ['stores'],
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-welcome-learn-more',
]
);
}
/**
* Add some rewrite rules for our archives (CPT, taxonomy archive)
*/
function movies_rewrite_rules() {
add_rewrite_rule(
'movies/([a-z]+)/page/?([0-9]{1,})/?$',
'index.php?post_type=movies&stores=$matches[1]&paged=$matches[2]',
'top'
);
add_rewrite_rule(
'movies/([a-z]+)/([a-z0-9_-]+)/?$',
'index.php?post_type=movies&stores=$matches[1]&movies=$matches[2]',
'top'
);
add_filter('query_vars', function($vars) {
$vars[] .= 'stores';
$vars[] .= 'stores-page';
return $vars;
});
}
/**
* Creates beautiful CPT permalinks, like site.com/movies/%term%/%post%/
*
* @param $permalink
* @param $post
*
* @return string|string[]
*/
function movies_post_permalinks($permalink, $post) {
// if it's not our CPT, return regular permalinks
if (strpos($permalink, '%stores%') === false) {
return $permalink;
}
// Get post taxonomy term
$terms = get_the_terms($post, 'stores');
// if a post has a term (store), push it into URL
if ( ! is_wp_error($terms) && ! empty($terms) && is_object($terms[0])) {
$term_slug = array_pop($terms)->slug;
} // if it doesn't, push something there, let it be d
else {
$term_slug = 'd';
}
return str_replace('%stores%', $term_slug, $permalink);
}https://stackoverflow.com/questions/66616081
复制相似问题