admin管理员组文章数量:1122846
I've renamed the default post type in my site 'Articles' using the following function:
// Replace Posts Label with Articles in Admin Panel
function change_post_menu_label() {
global $menu;
global $submenu;
$menu[5][0] = 'Articles';
$submenu['edit.php'][5][0] = 'Articles';
$submenu['edit.php'][10][0] = 'Add Articles';
echo '';
}
function change_post_object_label() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'Articles';
$labels->singular_name = 'Article';
$labels->add_new = 'Add Article';
$labels->add_new_item = 'Add Article';
$labels->edit_item = 'Edit Article';
$labels->new_item = 'Article';
$labels->view_item = 'View Article';
$labels->search_items = 'Search Articles';
$labels->not_found = 'No Articles found';
$labels->not_found_in_trash = 'No Articles found in Trash';
$labels->name_admin_bar = 'Add Article';
}
add_action( 'init', 'change_post_object_label' );
add_action( 'admin_menu', 'change_post_menu_label' );
I've added /articles/
to my custom permalink structure under Admin -> Settings -> Permalinks, so this now reads /articles/%postname%/
. The 'category base' and 'tag base' fields are blank.
Default category and tag taxonomies apply only to the default articles
post type.
CPTs all have 'rewrite' => array( 'with_front' => false )
declared.
My site also includes multiple CPTs (Stories, Walks, Cinemas) and multiple custom taxonomies (Borough, Area, etc), which have been applied only to the 'Cinema' post type.
I've registered the rewrite
property on the custom taxonomies using this code:
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
Current outcome/status:
- All post URLs (including default posts) are correct:
/[post-type]/[post-title]
- Default post tag and category archive urls are correct:
/articles/tag/[term]
//articles/category/[term]
- CPT custom taxonomy archive urls are incorrect:
/articles/[taxonomy]/[term]/
(should not include '/articles') get_category_link()
URLs are also wrong. Using the following code, I get/articles/[term]
, but the actual URL includes '/category' after '/articles':
$cats = get_the_category();
foreach($cats as $cat):
echo '<li><a href="'. get_category_link($cat->cat_ID) .'">';
echo $cat->name;
echo '</a></li>';
endforeach;
Desired outcome/status:
- All CPT singles and custom taxonomy archive URLs do not include
/articles/
? - All default singles and default taxonomy URLs retain
/articles/
? get_category_link()
produces the correct URL to show the archive and not a 404 error?
Many thanks in advance for any advice how to achieve this.
EDIT
In an attempt to remove /articles
from the custom taxonomy archive URLs I changed this line in the taxonomy declaration:
'rewrite' => $rewrite,
to this:
'rewrite' => $rewrite['with_front'] = false;,
As per Caleb's suggestion, but this turned all custom taxonomy archive URLs into search-type URLs, e.g. /?[taxonomy]=[term]
.
I've renamed the default post type in my site 'Articles' using the following function:
// Replace Posts Label with Articles in Admin Panel
function change_post_menu_label() {
global $menu;
global $submenu;
$menu[5][0] = 'Articles';
$submenu['edit.php'][5][0] = 'Articles';
$submenu['edit.php'][10][0] = 'Add Articles';
echo '';
}
function change_post_object_label() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'Articles';
$labels->singular_name = 'Article';
$labels->add_new = 'Add Article';
$labels->add_new_item = 'Add Article';
$labels->edit_item = 'Edit Article';
$labels->new_item = 'Article';
$labels->view_item = 'View Article';
$labels->search_items = 'Search Articles';
$labels->not_found = 'No Articles found';
$labels->not_found_in_trash = 'No Articles found in Trash';
$labels->name_admin_bar = 'Add Article';
}
add_action( 'init', 'change_post_object_label' );
add_action( 'admin_menu', 'change_post_menu_label' );
I've added /articles/
to my custom permalink structure under Admin -> Settings -> Permalinks, so this now reads /articles/%postname%/
. The 'category base' and 'tag base' fields are blank.
Default category and tag taxonomies apply only to the default articles
post type.
CPTs all have 'rewrite' => array( 'with_front' => false )
declared.
My site also includes multiple CPTs (Stories, Walks, Cinemas) and multiple custom taxonomies (Borough, Area, etc), which have been applied only to the 'Cinema' post type.
I've registered the rewrite
property on the custom taxonomies using this code:
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
Current outcome/status:
- All post URLs (including default posts) are correct:
/[post-type]/[post-title]
- Default post tag and category archive urls are correct:
/articles/tag/[term]
//articles/category/[term]
- CPT custom taxonomy archive urls are incorrect:
/articles/[taxonomy]/[term]/
(should not include '/articles') get_category_link()
URLs are also wrong. Using the following code, I get/articles/[term]
, but the actual URL includes '/category' after '/articles':
$cats = get_the_category();
foreach($cats as $cat):
echo '<li><a href="'. get_category_link($cat->cat_ID) .'">';
echo $cat->name;
echo '</a></li>';
endforeach;
Desired outcome/status:
- All CPT singles and custom taxonomy archive URLs do not include
/articles/
? - All default singles and default taxonomy URLs retain
/articles/
? get_category_link()
produces the correct URL to show the archive and not a 404 error?
Many thanks in advance for any advice how to achieve this.
EDIT
In an attempt to remove /articles
from the custom taxonomy archive URLs I changed this line in the taxonomy declaration:
'rewrite' => $rewrite,
to this:
'rewrite' => $rewrite['with_front'] = false;,
As per Caleb's suggestion, but this turned all custom taxonomy archive URLs into search-type URLs, e.g. /?[taxonomy]=[term]
.
1 Answer
Reset to default 0With @Caleb's help we've sorted this. The main problem was that the rewrite
parameter on the custom taxonomies was not set to false
. Doing this removed /articles/
from the permalinks to these archives.
We changed this:
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
To this:
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$rewrite['with_front'] = false; // new line
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
This means I was able to adjust the permalink settings to achieve the results I required using the Category/Tag base fields and the Custom Structure field under Settings -> Permalinks in the WP Admin as usual.
The remaining issues were actually due to a setting buried in the Yoast SEO plugin. They have moved the setting which strips the category base to Settings -> Categories - > Additional Settings.
Huge thanks to @Caleb for the help here.
本文标签: How to correctly edit permalink structures for both default and custom poststaxonomies
版权声明:本文标题:How to correctly edit permalink structures for both default and custom poststaxonomies? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736285261a1927423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
with_front
tofalse
. – Caleb Commented Oct 2, 2024 at 14:26$rewrite
variable, try adding this (untested):$rewrite['with_front'] = false;
. – Caleb Commented Oct 2, 2024 at 15:53/?[taxonomy]=[term]
– mtm Commented Oct 2, 2024 at 16:42$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] ); $rewrite['with_front'] = false;
– Caleb Commented Oct 3, 2024 at 15:48