1

I removed the /category/ slug using the Yoast SEO plugin

so now posts are like this: example.com/news/example

Instead of this: example.com/category/news/example

now my problem is that if I want a page to use the same slug category uses,

I can create the page but the category archive shows when I go to the URL.

I want the ability to decide which will be displayed, category archive or page when accessing URL

If anyone can help me doing that by code or anything, that I will be able to use the same slug for both but display the page over the category archive it will help a lot

Thank you for reading, Shay.

2
  • By "display the page over the category" do you mean you want to show both page content and archive content together all at once? If so, you can edit whichever theme file is being used. It may be simply archive.php or could be category.php or something more specific, depending on the theme.
    – WebElaine
    Commented Apr 26, 2017 at 15:30
  • Thank you for replying - I ment that posts will be displayed in this slug: /catname/postname/ but if you go to /catname/ you will see the page
    – Shay S
    Commented Apr 26, 2017 at 20:13

1 Answer 1

2

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.

4
  • can you please show me an example in your 3 method's code? I don't know where I should insert the page id and such
    – Shay S
    Commented May 5, 2017 at 11:30
  • The code in example 3 is ready to use. You don't need to put in a page id etc. For every Category, add a Page, so that the Category slug matches the Page slug exactly. Then WP will use the Page instead of the Category Archive at that Page slug.
    – WebElaine
    Commented May 8, 2017 at 13:14
  • Option #3 works fine. Load "normal" page instead category page Thanks
    – Fer
    Commented Feb 9, 2021 at 19:23
  • @Fer I converted your solution into a comment. Do not post comments as solutions/answers. Low rep users who do this get flagged as spammers and banned by the automated systems
    – Tom J Nowell
    Commented Feb 9, 2021 at 19:48

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