最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

slug - Display page over category archive

programmeradmin1浏览0评论

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

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

Instead of this: example/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.

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

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

Instead of this: example/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.

Share Improve this question asked Apr 26, 2017 at 14:43 Shay SShay S 693 silver badges6 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论