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:

  1. All CPT singles and custom taxonomy archive URLs do not include /articles/?
  2. All default singles and default taxonomy URLs retain /articles/?
  3. 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:

  1. All CPT singles and custom taxonomy archive URLs do not include /articles/?
  2. All default singles and default taxonomy URLs retain /articles/?
  3. 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].

Share Improve this question edited Oct 4, 2024 at 8:45 mtm asked Oct 2, 2024 at 13:43 mtmmtm 438 bronze badges 9
  • Seems that the taxonomy registration does not set with_front to false. – Caleb Commented Oct 2, 2024 at 14:26
  • Thanks. Yes I thought that might be a partial answer to the question. I'm not sure how to implement it into the variable though — any advice? – mtm Commented Oct 2, 2024 at 15:44
  • After initially setting the $rewrite variable, try adding this (untested): $rewrite['with_front'] = false;. – Caleb Commented Oct 2, 2024 at 15:53
  • Thanks Caleb. For reasons I can't fathom, this changes the archive URLs to search result URLs, e.g. /?[taxonomy]=[term] – mtm Commented Oct 2, 2024 at 16:42
  • 1 $rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] ); $rewrite['with_front'] = false; – Caleb Commented Oct 3, 2024 at 15:48
 |  Show 4 more comments

1 Answer 1

Reset to default 0

With @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