admin管理员组文章数量:1125739
I have a custom post type "location".
add_action("init", "register_custom_post_types");
function register_custom_post_types() {
register_post_type("location", array(
"supports" => array("title", "editor"),
"rewrite" => array(
"slug" => "location"
),
"has_archive" => false,
"public" => true,
"show_in_rest" => true,
"menu_icon" => "dashicons-location-alt"
));
}
This gives me the following urls:
- /location/berlin
- /location/london
- /location/paris
Now I would like to create subpages for each location. For example:
- /location/berlin/visit
- /location/berlin/hotels
- /location/berlin/about
How can I do that? Assign pages to a CPT? A new post type?
I have a custom post type "location".
add_action("init", "register_custom_post_types");
function register_custom_post_types() {
register_post_type("location", array(
"supports" => array("title", "editor"),
"rewrite" => array(
"slug" => "location"
),
"has_archive" => false,
"public" => true,
"show_in_rest" => true,
"menu_icon" => "dashicons-location-alt"
));
}
This gives me the following urls:
- /location/berlin
- /location/london
- /location/paris
Now I would like to create subpages for each location. For example:
- /location/berlin/visit
- /location/berlin/hotels
- /location/berlin/about
How can I do that? Assign pages to a CPT? A new post type?
Share Improve this question asked Jan 26, 2024 at 22:29 LucaLuca 31 bronze badge2 Answers
Reset to default 1Add the parameter hierarchical
to the array with value true
. This will allow you to create child posts on your CPT.
Source: https://developer.wordpress.org/reference/functions/register_post_type/#parameters
To create subpages like /location/berlin/visit, /location/berlin/hotels, and /location/berlin/about for each of your custom post types, you need to use WordPress rewrite rules and possibly custom query variables. This approach allows you to handle custom URL structures and associate them with your custom post type "location".
Here's a step-by-step guide to achieve this:
Step 1: Define Custom Rewrite Rules You need to add custom rewrite rules to handle your new URL structure. This can be done by hooking into the init action and using the add_rewrite_rule function.
function wpb_add_custom_rewrite_rules() {
add_rewrite_rule(
'^location/([^/]+)/([^/]+)/?$',
'index.php?location=$matches[1]&wpb_location_page=$matches[2]',
'top'
);
}
add_action('init', 'wpb_add_custom_rewrite_rules');
In this rule, ^location/([^/]+)/([^/]+)/?$ is a regular expression that matches URLs of the format /location/{location-name}/{subpage}/. The location and wpb_location_page are query variables that will be used to determine the actual content to display.
Step 2: Add Custom Query Variables You need to let WordPress know about the new query variable wpb_location_page.
function wpb_add_query_vars($vars) {
$vars[] = 'wpb_location_page';
return $vars;
}
add_filter('query_vars', 'wpb_add_query_vars');
Step 3: Handle The Custom Query
Now, you need to hook into the template redirect action to handle these custom queries and serve the appropriate content.
function wpb_location_template_redirect() {
$location = get_query_var('location');
$location_page = get_query_var('wpb_location_page');
if ($location && $location_page) {
// Load your custom template or perform any logic here
// For example:
include(get_stylesheet_directory() . '/location-subpage.php');
exit;
}
}
add_action('template_redirect', 'wpb_location_template_redirect');
Step 4: Flush Rewrite Rules
After adding these codes, remember to flush your rewrite rules. You can do this by visiting the Permalinks settings page in the WordPress admin and simply clicking "Save Changes".
Optional: Creating the Subpage Content
The actual content of the subpages (e.g., visit, hotels, about) can be handled in various ways, depending on your specific requirements. You might use custom fields, create a taxonomy for subpage types, or even hard-code the content in the template files. This part is quite flexible and depends on how you want to manage and display these subpages.
本文标签: customizationSubpage for Custom Post Type
版权声明:本文标题:customization - Subpage for Custom Post Type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736674748a1947119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论