Wordpress - Force Wordpress to Show Pages Instead of Category
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 );
I created the following, fairly simple function in order to make the pages a higher importance than categories when loading the content.
(Note: this may not the best, but a simple way. I found an issue, when using the Advanced Custom Fields plugin, it doesn't insert the custom fields neither of the old, nor of the new page by default. Adding get_queried_object()->ID
as the ACF get_field()
's second parameter solved this. Test it carefully in your WP environment.)
function loadPageFirst() {
// get the actual category
$actualCategory = get_category( get_query_var('cat') );
// get the page with the same slug
$matchingPage = get_page_by_path( $actualCategory->slug );
// If no match, load the normal listing template and exit (edit if you are using a custom listing template, eg. category.php)
if (!$matchingPage) {
include( get_template_directory() . '/archive.php');
die();
}
// Make a new query with the page's ID and load the page template
query_posts( 'page_id=' . $matchingPage->ID );
include( get_template_directory() . '/page.php');
die();
}
add_filter( 'category_template', 'loadPageFirst' );
You can include it in your theme's functions.php. This looks to work OK with the Yoast SEO plugin.