4

So I am working on a large media site using Wordpress. I want to create customised category pages, rather than using a blanket template throughout.

I am aware that I can copy the php files, rename them and customise those. But I have come across an easier way, especially in terms of allowing non-coding types to make changes. It just seems a little too easy...

So let's say my current permalinks/URLs read like this:

POSTS: domain.com/2016/10/postname.html
CATEGORIES: domain.com/c/category-name/
PAGES: domain.com/page-name/

I have discovered that I can create a page and give it the permalink /c/, thus it would look like this:

domain.com/c/

I have discovered that if I create a child page, using /c/ as the parent, it looks like this:

domain.com/c/page-name/

If I give this page the same SLUG as a category, and then click through to the page, the page overrides the category. To be more specific, let's say I have a category called videos, then I create a page under the /c/ parent page also called videos, this happens:

The PAGE at /c/videos/
overrides the CATEGORY at /c/videos.

It just seems too good to be true? Can anyone see any reason why I wouldn't proceed like this, especially when the site is going to be used by non-coding types in the future who'd like to be able to make easy changes using the dashboard as opposed to the backend?

Thanks in advance.

BHIZ
  • 41
  • 2
  • Hmmm, the link for the video category is `domain.com/videos` and the link for the child page is `domain.com/c/videos`. Right? – Andy Tschiersch Oct 06 '16 at 06:19
  • Sorry, I should have said that the permalink for categories has been changed to /c/. So before I had the child page, the link for the video category is domain.com/c/videos and once I had the child page, the link for the child page is domain.com/c/videos. And if I click through to that URL, I visit the child page, not the category archive. – BHIZ Oct 07 '16 at 00:24
  • I've been trying to do this, but in my install the category page overrides the "page" page. Also tried removing the category slug with Yoast seo plugin... same problem with matching. Did visit the permalinks page (and saved) to flush rewrite rules, but no go... – yezzz Nov 18 '16 at 20:32

1 Answers1

2

This require a small filter to be added in your theme's function.php file. I have taken this code from another answer on StakeOverflow and you can read it here

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' );
Rahul Dhangar
  • 78
  • 1
  • 11