It seems to be a common question as to how to override an auto-generated category page with a manually-created Page when both exist at the same location
for example, /xxxx/ and /yyyy/ and /zzzz/ are categories, but for one of them, you want to use a custom Page (while leaving the others alone). So you create a Page with the slug "xxxx", but Wordpress chooses to serve the category instead of your Page
I found a lot about this online however all the existing solutions only work with classic themes
this seems to be the most common solution:
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' );
but page.php
and archive.php
don't exist on block themes
attempting this code unmodified will result in a 5XX error
attempting to replace the reference to archive.php
with a reference to the theme's templates/archive.html
will result in the raw content of archive.html
being returned, which renders as a blank page
I'm currently working around this by giving the Pages an alternate slug & utilizing Cloudflare invisible rewrites to send traffic to the alternate slug, so that the visitor never sees the alternate slug... but that's not an elegant solution.