0

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.

  • 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 Apr 26 '17 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 Apr 26 '17 at 20:13
1

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.

| improve this answer | |
  • 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 May 5 '17 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 May 8 '17 at 13:14

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.