对于任何感兴趣的人来说,这是我问题的完整解决方案。到目前为止,效果相当好。
我使用%postname%
将我的自定义分类法作为我的Brand
和自定义帖子类型作为我的Product
.
我的URL如下所示example.org/some-brand/some-product
而我可以在上显示概述example.org/some-brand/
.
我正在使用高级自定义字段Pro插件使我的所有页面都可编辑。功能wp323_get_template_file
实现@Milo建议的内容,因为对页面或具有自定义页面模板的页面的所有请求都被重定向到了single。php。现在页面按页面显示。php,如果有自定义页面模板,则会显示正确的模板。
每一个Product
必须链接到Brand
, 否则URL将包含/other/
而不是品牌。
希望这对某人有所帮助,我花了几个晚上才完成。感谢@Milo。
<?php
function wpd_fix_requests( $request ){
// Written by @Milo: https://wordpress.stackexchange.com/questions/215987/remove-base-slug-in-cpt-ct-use-ct-in-permalink
// if it\'s a brand term request
// see if a brand term exists by this name with get_term_by
// if not, reset the query to a post or page with name
if( array_key_exists( \'brand\' , $request )
&& ! array_key_exists( \'post_type\' , $request )
&& ! get_term_by( \'slug\', $request[\'brand\'], \'brand\' ) ){
$request[\'name\'] = $request[\'brand\'];
$request[\'post_type\'] = array( \'post\', \'page\' );
unset( $request[\'brand\'] );
}
// if this is a review request
// add page to post type in case it\'s a child page
if( array_key_exists( \'review\', $request ) ){
$request[\'post_type\'] = array( \'review\', \'page\' );
}
// return request vars
return $request;
}
add_filter( \'request\', \'wpd_fix_requests\' );
function brand_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%brand%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'brand\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'other\';
return str_replace(\'%brand%\', $taxonomy_slug, $permalink);
}
add_filter(\'post_link\', \'brand_permalink\', 1, 3);
add_filter(\'post_type_link\', \'brand_permalink\', 1, 3);
/**
* Code below is from https://wordpress.stackexchange.com/questions/57493/custom-taxonomy-specific-to-a-custom-post-type
* A custom taxonomy is created and linked to CPT \'review\'.
* The goal is to create permalinks containing the taxonomy + CPT post name, e.g. /some-brand/xyz-review/
*/
function custom_brand_taxonomy() {
register_taxonomy(
\'brand\', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
\'review\', //post type name
array(
\'hierarchical\' => true,
\'label\' => \'Brand\', //Display name
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'/\', // This controls the base slug that will display before each term
\'with_front\' => false // Don\'t display the category base before
)
)
);
}
add_action( \'init\', \'custom_brand_taxonomy\');
/**
* Creating a function to create our CPT
*
*/
function xyz_custom_post_types() {
// Set options for Custom Post Type REVIEW
$review_args = array(
\'label\' => __( \'review\', \'mythemexyz\' ),
\'description\' => __( \'Descrption review bla bla\', \'mythemexyz\' ),
\'labels\' => array(
\'name\' => _x( \'reviewe\', \'Post Type General Name\', \'mythemexyz\' ),
\'singular_name\' => _x( \'review\', \'Post Type Singular Name\', \'mythemexyz\' ),
\'menu_name\' => __( \'reviewe\', \'mythemexyz\' ),
\'parent_item_colon\' => __( \'Parent review\', \'mythemexyz\' ),
\'all_items\' => __( \'Alle reviewe\', \'mythemexyz\' ),
\'view_item\' => __( \'review ansehen\', \'mythemexyz\' ),
\'add_new_item\' => __( \'review erstellen\', \'mythemexyz\' ),
\'add_new\' => __( \'Erstellen\', \'mythemexyz\' ),
\'edit_item\' => __( \'review bearbeiten\', \'mythemexyz\' ),
\'update_item\' => __( \'review aktualisieren\', \'mythemexyz\' ),
\'search_items\' => __( \'review suchen\', \'mythemexyz\' ),
\'not_found\' => __( \'Nicht gefunden\', \'mythemexyz\' ),
\'not_found_in_trash\' => __( \'Nicht in Papierkorb gefunden\', \'mythemexyz\' ),
),
// Features this CPT supports in Post Editor
\'supports\' => array( \'title\', \'editor\', \'revisions\', \'custom-fields\', \'page-attributes\' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 99,
\'can_export\' => true,
\'rewrite\' => array( \'slug\' => \'%brand%\', \'with_front\' => false ),
\'has_archive\' => \'review\',
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
);
// Registering your Custom Post Type
register_post_type( \'review\', $review_args );
}
/* Hook into the \'init\' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( \'init\', \'xyz_custom_post_types\', 0 );
/**
* Use single_template filter to properply redirect to page.php and custom page templates
*/
function wp323_get_template_file($single_template) {
global $post;
$page_custom_template = get_post_meta( $post->ID, \'_wp_page_template\', true );
if ($post->post_type == \'page\') {
if($page_custom_template != \'default\') {
$single_template = dirname( __FILE__ ) . \'/\' . $page_custom_template;
}
else {
$single_template = dirname( __FILE__ ) . \'/page.php\';
}
}
return $single_template;
}
add_filter( \'single_template\', \'wp323_get_template_file\' );