我需要你的帮助。我需要创建此线程中提到的类似结构,以下是我的项目要求的详细信息:
我有三种自定义帖子类型,比如公司目录、视频和文章。每个都有一个共同的分类法,我们称之为税。
我要设置URL的方式是:
“tax\\u term”是属于通用分类法的术语名称。
公司目录存储公司配置文件的信息。这将具有默认URL,即站点。com/公司目录
对于CPT“视频”-我希望URL是站点。com/company directory/tax\\u term/video/post\\u slug
对于CPT“文章”-网站。com/company directory/tax\\u term/articles/post\\u slug
我试过了exact solution @RachelCarden provided 它与CPT“视频”一起工作,但CPT“文章”正在重定向到主页(301)。下面是我正在处理的代码:
add_action( \'init\', \'my_website_add_rewrite_tag\' );
function my_website_add_rewrite_tag() {
//Matching the url with the custom structure
//Custom URL for videos
add_rewrite_rule( \'^company-directory/([^/]*)/([^/]*)/([^/]*)/?\',\'index.php?company-video=$matches[3]\',\'top\' );
//Custom URL for reports
add_rewrite_rule( \'^company-directory/([^/]*)/([^/]*)/?\',\'index.php?report-presentation=$matches[2]\',\'top\' );
}
// this filter runs whenever WordPress requests a post permalink, i.e.
get_permalink(), etc.
// we will return our custom permalink for \'videos\' and \'reports\'.
//\'company-directory\' is already good to go since we defined its rewrite slug in the CPT definition.
add_filter( \'post_type_link\', \'my_website_filter_post_type_link\', 1, 4 );
function my_website_filter_post_type_link( $post_link, $post, $leavename, $sample ) {
switch( $post->post_type ) {
case \'company-video\':
if ( $term = array_shift( wp_get_object_terms( $post->ID, \'trading-symbol\' ) ) ) {
if ( isset( $term->slug ) ) {
// create the new permalink
$post_link = home_url( user_trailingslashit( \'company-directory/\' . $term->slug . \'/videos/\' . $post->post_name ) );
}
}
break;
case \'report-presentation\':
if ( $term2 = array_shift( wp_get_object_terms( $post->ID, \'trading-symbol\' ) ) ) {
if ( isset( $term2->slug ) ) {
// create the new permalink
$post_link = home_url( user_trailingslashit( \'company-directory/\' .$term2->slug. \'-report-presentation/\' . $post->post_name ) );
}
}
break;
}
return $post_link;
}
我希望有人能指导我解决这个问题。谢谢