Display page over category archive

Option #1

Add a theme file for each category, instead of adding a Page for each category. You would create a file called category-news.php which would only display for the news category at /news/, plus files for each other category, such as category-other.php which would display for the other category at /other/. Then just place whatever content you want in the template files. Note that categories have a “description” built into WordPress so you can edit that portion in wp-admin without having to always edit your theme file when you want to make changes.

Option #2

Alternatively, you could create one theme file called category.php and code it this way:

<?php
get_header();
// get the current category
$category = get_category(get_query_var('cat'));
// get the page whose slug matches this category's slug
$page = get_page_by_path($category->slug);
// display the page title as an h1 ?>
<h1><?php echo $page->post_title; ?></h1><?php
// display the page content
echo $page->post_content;
get_footer();
?>

Option #3

Or, in your theme’s functions.php:

<?php
add_filter('request', function(array $query_vars) {
    // do nothing in wp-admin
    if(is_admin()) {
        return $query_vars;
    }
    // if the query is for a category
    if(isset($query_vars['category_name'])) {
        // save the slug
        $pagename = $query_vars['category_name'];
        // completely replace the query with a page query
        $query_vars = array('pagename' => "$pagename");
    }
    return $query_vars;
});
?>

If you choose option 2 or 3, the result is the same – WP will look for a Page with a slug that matches the category slug. You can then include whatever content you want in each Page and it will appear on the category. You will probably also want to include a sidebar or some other list of posts within that category so people will be able to go from the category to one of the individual posts. With option #2 you would add the “show other posts” code in category.php. With option #3 you would need to create a custom Page Template (example tpl-category.php) and apply it to each Page.

Leave a Comment