Set custom pages instead of category/archive pages

1.1k views Asked by At

I'm trying to set custom pages instead of the default category/archive pages of Wordpress.

I've copied the following script into the theme function.php, I mention that the site uses The7 Theme.

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' );

I took it from here, it's bencergazda's solution.

Now, after the script, if I create a page, which has the same url as a category page it automatically replaces it.

The problem is that the script is limited to the main (parent) category.

I want to create a child page (let's say example.com/cars/european/german) that automatically replaces the same child category page.

My question is how to modify the script to include category children.

3

There are 3 answers

1
user1492189 On BEST ANSWER

Run a try with this:

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
    global $post; $post->ID = $matchingPage->ID;
    query_posts( 'page_id=' . $matchingPage->ID );
    include( get_template_directory() . '/page.php');
    die();
}
add_filter( 'category_template', 'loadPageFirst' );
1
Tom On

You can easily do this by using templates. Use a child theme and make a template called category-$slug.php and replace $slug for the slug you want to make a page for. You can also just make a category.php. For more options check this template hierarchy.

1
Inzamam Ul Haq On

Here is the updated version of the code that works for both Categories and Subcategories. I used it with GeneratePress Theme and it works fine.

function loadPageFirst($template) {
// get the actual category
$actualCategory = get_queried_object();
$actualCategoryId = $actualCategory->term_id;

// check if the actual category is a subcategory
if ($actualCategory->parent != 0) {
    // get the parent category and its slug
    $parentCategory = get_term($actualCategory->parent, 'category');
    $parentCategorySlug = $parentCategory->slug;

    // check if there's a subcategory page template for the parent category
    $matchingPage = get_page_by_path($parentCategorySlug . '/' . $actualCategory->slug);
    if ($matchingPage) {
        // set the query and load the subcategory page template
        global $post;
        $post->ID = $matchingPage->ID;
        query_posts('page_id=' . $matchingPage->ID);
        $newTemplate = locate_template(array('page-' . $parentCategorySlug . '-' . $actualCategory->slug . '.php', 'page-' . $actualCategory->slug . '.php', 'page.php'));
        return $newTemplate ? $newTemplate : $template;
    }
}

// if there's no subcategory page template, fall back to the parent category page template
$matchingPage = get_page_by_path($actualCategory->slug);
if (!$matchingPage) {
    return $template;
}

// set the query and load the parent category page template
global $post;
$post->ID = $matchingPage->ID;
query_posts('page_id=' . $matchingPage->ID);
$newTemplate = locate_template(array('page-' . $actualCategory->slug . '.php', 'page.php'));
return $newTemplate ? $newTemplate : $template;

} add_filter('category_template', 'loadPageFirst');