4

I am trying to create a SILO structure on one of my WordPress site.

I have always checked this option on Yoast SEO plugin "Strip the category base (usually /category/) from the category URL."

So my category pages have this URL: sitename.com/apple/ (where "apple" is the category page)

Now I want to create a new "page" with the same slug, apple.

I can create a new "apple" page easily, however, when I go to sitename.com/apple/ the category page is displayed -- instead of the newly created page.

My question: Is there a way to "override" the default priority of category over a page? I'd like to show page because I will have control over it -- I can customize, add more content, and change them easily (plus, pages are going to help me structure my site better)

Appreciate your help!

5

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 );
  • Milo, this is exactly what I wanted! I can't believe this was so easy and possible. Thank you very much! There is a problem though. The sub-category pages are pointing to 404. Is there a way to fix this? Additional details: For my sites, breadcrumbs are required. So here is the format: Home -> Cat 1 -> Sub cat 2 -> post. The cat 1 is a PAGE now, but sub-cat 2 points to a 404 page. – Sid Jul 11 '13 at 4:39
  • you'd have to create child pages under the parent pages for subcategories. – Milo Jul 11 '13 at 4:42
  • Genius, that's who you are. Thanks a lot. This does take a lot of time and effort. I think I will do it anyway. Still, I'd like to know if I can point only parent cat to PAGE while sub-cat shows the default cat page? – Sid Jul 11 '13 at 6:41
  • in the above function, check if $term->parent equals 0. child categories will have a category ID as $term->parent, in top level categories it will just be zero. – Milo Jul 11 '13 at 15:10
  • I understand I have to use if ($term->parent == 0) but I am not sure where to put this code. I have spent some time Googling but cannot find a way to get this working. Really appreciate if you can revise the code. :) – Sid Jul 11 '13 at 19:27
2

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.

  • I believe you don't need to include archive.php and die() — depending on your theme, this won't work. You can just return instead. (But thanks a ton for the code!) – Nabha Apr 11 '18 at 23:56
  • Didn't fully work in my case, page title was messed up e.g. – kero Nov 22 '18 at 10:38
1

There is a super simple and perfectly working answer here: https://stackoverflow.com/questions/32310453/make-wordpress-use-the-page-instead-of-category

For the sack of completeness:

Background

  • example.com is the domain
  • I have a WP page called "foobar" with content
  • I have a WP post category called "foobar"
  • I have a WP post entitled "fun things to do with foobars", and the category is set to "foobar"

Expectations

  • When I go to example.com/foobar, I want to see the page about foobars, not a WP category page that shows all blog posts with that category.
  • When I go to the blog post about fun things, the URL is example.com/foobar/fun-things-to-do-with-foobars/

Setup

Here's my setup (it does not require any additional plugins or code edits) A couple of installed plugins are WP Rocket and Yoast on which I'll focus in this thread.

  1. WP Dashboard->Settings->Permalinks

  2. Select "Custom Structure" and enter /%category%/%postname%/ in the field

  3. On the same page, change "Category base" to a single dot. "." (no quotes, just a single dot/period/full stop

I have Yoast installed, which I had set up to override the permalink settings, but I believe this messed up my task at hand.

  1. WP Dashboard->SEO->Advanced->Permalinks(tab)

  2. "Change URLs->Strip the category base..." = Keep

  3. Then you need to clear the cache. I use WP Rocket for caching, so I flushed the cache and everything now works the way it should.

Edit: make sure you save your changes after steps 3 and 5

0

Depends. If you're trying to set 2 different page with the same URL it's not a good idea. But if you do not care about category pages you can redirect them to your pages.

 RedirectMatch 301 ^/category/(.*)$ /$1

This code goes in .htaccess and redirect /category/page to /page/

  • Thanks! However, as mentioned before, I don't have the category base attached to the URL. And I can't use the traditional 301 as both the URLs are the same. – Sid Jul 10 '13 at 20:14
  • ?? Sorry I do not get it – JMau Jul 10 '13 at 20:20
  • The URL of the page and the URL of the category are the same. I am doing this "intentionally" to create a SILO structure, but Wordpress shows category page over the new page. I hope this makes sense. – Sid Jul 11 '13 at 2:20
0

The "WordPress way" to do this would be to create a custom php template for your category page, and apply your customizations there, rather than by creating a unique "Page."

There are also numerous plugins available that allow you to add images and other custom fields to the category archive page, which may be a better option if you would rather not modify theme files.

Alternatively, I'd suggest adding a category base that is perhaps one of your primary target keywords

  • The problem is, I believe, Wordpress treats "pages" differently. They rank better than the traditional category pages. Many SEOs are doing what I have asked above -- removing the category completely and replacing it with pages. – Sid Jul 11 '13 at 2:23
  • You probably should elaborate how to add this page template (hint: template hierarchy), etc. – kaiser Jul 11 '13 at 2:24
  • @Sid - if the content is identical, why would a category archive rank differently than a page? – Milo Jul 11 '13 at 3:10
  • @Milo - The content will not be identical, only the URL will be. Pages will have lot more structured content and links pointing to related material. – Sid Jul 11 '13 at 4:02
  • @Sid my point was you could get the same results via a category template, by pulling additional content from elsewhere. a quick way to achieve that is the Advanced Custom Fields plugin, which lets you add additional content to taxonomy term admin pages- wysiwyg editors, relationship fields, etc.. – Milo Jul 11 '13 at 4:37
0

First strip the category in yoast No-index the category in yoast but before doing that the permalink structure should be set as /%category%/%postname%/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.