I am trying to create a SILO structure on one of my WordPress site.
I have always checked this option on Yoast SEO plugin “Strip the category base (usually /category/) from the category URL.”
So my category pages have this URL: sitename.com/apple/ (where “apple” is the category page)
Now I want to create a new “page” with the same slug, apple.
I can create a new “apple” page easily, however, when I go to sitename.com/apple/ the category page is displayed — instead of the newly created page.
My question: Is there a way to “override” the default priority of category over a page? I’d like to show page because I will have control over it — I can customize, add more content, and change them easily (plus, pages are going to help me structure my site better)
Appreciate your help!
One possible solution is to not change the category base and leave as-is, but instead modify any output of a category link to strip the category base via a filter. This of course will result in a 404 if you haven’t created a page in place of where these links point, so creating a page for every category is required.
function wpa_alter_cat_links( $termlink, $term, $taxonomy ){
if( 'category' != $taxonomy ) return $termlink;
return str_replace( '/category', '', $termlink );
}
add_filter( 'term_link', 'wpa_alter_cat_links', 10, 3 );
You’ll probably want to test this thoroughly for any side-effects, use at your own risk!
EDIT – altering just top level category links:
function wpa_alter_cat_links( $termlink, $term, $taxonomy ){
if( 'category' == $taxonomy && 0 == $term->parent ){
return str_replace( '/category', '', $termlink );
}
return $termlink;
}
add_filter( 'term_link', 'wpa_alter_cat_links', 10, 3 );