我正在一个Wordpress项目中工作,该项目有一个称为“菜单”的自定义帖子类型,并且我希望根据url目录根据自定义字段显示不同的内容。这部分很容易归档,但是我很难理解如何设置网址。 如何为相同的自定义帖子类型设置两个不同的网址?
url1 = /菜单/
url2 = / directory2 /菜单/
这是我的代码:
//MENU POST Type
add_action('init', 'my_custom_menu');
function my_custom_menu()
{
$labels = array(
'name' => _x('MENU', 'post type general name'),
'singular_name' => _x('MENU', 'post type singular name'),
'add_new' => _x('新しくMENUを書く', 'menu'),
'add_new_item' => __('MENU記事を書く'),
'edit_item' => __('MENU記事を編集'),
'new_item' => __('新しいMENU記事'),
'view_item' => __('MENU記事を見る'),
'search_menu' => __('MENU記事を探す'),
'not_found' => __('MENU記事はありません'),
'not_found_in_trash' => __('ゴミ箱にMENU記事はありません'),
'parent_item_colon' => '',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'with_front' => false,
'slug' => 'menu',
),
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail', 'post_thumbnails'),
'has_archive' => true,
);
register_post_type('menu', $args);
}
// Return correct permalink for CPT with CT in slug
function menu_links($post_link, $post = 0)
{
$terms = wp_get_object_terms($post->ID, 'locationcat');
if ($post->post_type === 'menu') {
return home_url('menu/' . $terms[0]->slug . '/' . $post->ID . '/');
} else {
return $post_link;
}
}
add_filter('post_type_link', 'menu_links', 1, 3);
// Get the Post ID from permalink structure base/CT/Post_ID
function menu_rewrites_init($post_link, $post = 0)
{
$placesTerms = get_terms([
'taxonomy' => 'menucat',
'hide_empty' => false
]);
$placesArray = [];
foreach($placesTerms as $term){
array_push($placesArray, $term->slug);
}
$places = implode("|", $placesArray);
// add_rewrite_rule('menu/.*/([0-9]+)?$', 'index.php?post_type=menu&p=$matches[1]', 'top');
add_rewrite_rule('menu\/([A-Za-z0-9\-]+)?\/([0-9]+)?$', 'index.php?post_type=menu&p=$matches[2]', 'top');
add_rewrite_rule('menu\/(?!.*' . $places . ')([A-Za-z0-9\-]+)?\/?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&locationcat=$matches[1]', 'top');
add_rewrite_rule('menu\/([A-Za-z0-9\-]+)?\/?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&menucat=$matches[1]', 'top');
add_rewrite_rule('menu\/([A-Za-z0-9\-]+)?\/([A-Za-z0-9\-]+)?\/?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[4]&locationcat=$matches[1]&menucat=$matches[2]', 'top');
}
add_action('init', 'menu_rewrites_init');
// make taxonomy
add_action('init', 'create_locationcat_taxonomy', '0');
function create_locationcat_taxonomy()
{
$taxonomylabels = array(
'name' => _x('locationcat', 'post type general name'),
'singular_name' => _x('locationcat', 'post type singular name'),
'search_items' => __('locationcat'),
'all_items' => __('locationcat'),
'parent_item' => __('Parent Cat'),
'parent_item_colon' => __('Parent Cat:'),
'edit_item' => __('Cat記事を編集'),
'add_new_item' => __('Cat記事を書く'),
'menu_name' => __('LOCATIONカテゴリー'),
);
$args = array(
'labels' => $taxonomylabels,
'hierarchical' => true,
'has_archive' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'menu'),
);
register_taxonomy('locationcat', 'menu', $args);
}
add_action('init', 'create_menucat_taxonomy', '0');
function create_menucat_taxonomy()
{
$taxonomylabels = array(
'name' => _x('menucat', 'post type general name'),
'singular_name' => _x('menucat', 'post type singular name'),
'search_items' => __('menucat'),
'all_items' => __('menucat'),
'parent_item' => __('Parent Cat'),
'parent_item_colon' => __('Parent Cat:'),
'edit_item' => __('Cat記事を編集'),
'add_new_item' => __('Cat記事を書く'),
'menu_name' => __('MENUカテゴリー'),
);
$args = array(
'labels' => $taxonomylabels,
'hierarchical' => true,
'has_archive' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'with_front' => false
)
);
register_taxonomy('menucat', 'menu', $args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_menu_category()
{
$menu_category_args = get_taxonomy('locationcat'); // returns an object
$menu_category_args->show_admin_menu = true;
$menu_category_args->rewrite['slug'] = 'menu';
$menu_category_args->rewrite['with_front'] = false;
register_taxonomy('locationcat', 'menu', (array) $menu_category_args);
}
add_action('init', 'rewrite_menu_category', 11);