admin管理员组文章数量:1122846
I don't find a way to have a template for my root category page ??
my archive.php file works great for any link such as:
etc
but for /
I can not find the file who could edit that url ? at the moment I just have a 404...
Any idea ? I tried to add a template archive-slug.php without success :(
Thanks a lot for your help !
I don't find a way to have a template for my root category page ??
my archive.php file works great for any link such as:
http://adress.com/category/category-name
etc
but for http://adress.com/category/
I can not find the file who could edit that url ? at the moment I just have a 404...
Any idea ? I tried to add a template archive-slug.php without success :(
Thanks a lot for your help !
Share Improve this question asked Jul 15, 2017 at 19:29 thebigEthebigE 331 silver badge6 bronze badges 3- 1 such a template does not exist. what would you expect that a template like that should show? – Michael Commented Jul 15, 2017 at 19:43
- Hi Michael, thanks for answering. It should show all post from all categories, for SEO purpose I ve been told category should have a parent page ! How can it be done ? – thebigE Commented Jul 15, 2017 at 19:59
- 1 'all posts of all categories' would be the 'posts page', which is either the home page or a page which you can set via 'settings - reading' ... – Michael Commented Jul 15, 2017 at 20:55
4 Answers
Reset to default 2Today I myself was faced with such a need. Google didn't help. I found a solution on my own. There is a hook — 'template_include'.
Let's add it to the functions.php file.
add_filter( 'template_include', 'my_template' );
function my_template( $template ) {
if( $_SERVER['REQUEST_URI'] === '/category/' || $_SERVER['REQUEST_URI'] === '/category' ){
return get_stylesheet_directory() . '/root_category.php';
} else {
return $template;
}
}
That's all. All that remains is to create the file root_category.php
in your theme and fill it out at your discretion.
Please note that the request is checked via $_SERVER['REQUEST_URI']
. There may be an easier way, but I haven't found it.
Thanks to this hook, you can even disassemble the entire topic into parts and create templates even for individual pages by ID and anything else.
P.S. I forgot to clarify that the 404 status will remain the same. In my case this is exactly what is required. If you have other preferences, you may need to find out how to register it via taxonomy, or go the other way and add header("HTTP/1.0 200 OK");
at the beginning of your template and use the hook - document_title
(to change the title).
You can't find it because it doesn't exist. There is no page at that location.
In WordPress everything is either an archive of posts or a post of some type. Pages are posts of type page
etc. Date archives are archives of posts
So if you went to /category
what would it show? For this reason there is nothing there to show. You would need to write a rewrite rule, manually load a template, and that's assuming it doesn't interfere with other rewrite rules
Your home page already shows posts from all the categories. The reason that you can't find anything in that path is that /category/
is a base term for categories, the same way /tag/
is for tags. You can check this in the permalink page, if you navigate to Settings > Permalinks
.
There is only 1 condition that you can have a page that list every post from every category, and it's when you have a static home page. In that case, if you navigate to http://example.com/blog/
you will see a list of every post on your website. If you wish, you can redirect http://example.com/category/
to http://example.com/blog/
.
However, be careful not to redirect any other path such as http://example.com/category/example/
, and make sure you have a static page set as home page, or you will get stuck in an infinite loop.
But as @Tom already mentioned, a quarter of the internet runs on WordPress, and they don't have a SEO problem because of this. So, you can just forget about it ;)
Curiosity and insomnia helped me find a much more work-friendly option. Actually, insomnia initially prevented me from immediately finding the correct answer.
The right approach:
- Create a page - Pages > Add New Page. With any title you like;
- Save and close it;
- Go to Quick Edit and change the Slug to
category
; - Create a
page-category.php
template file inside your theme and fill it out as you wish.
Voila.) Coffee works wonders.
本文标签: categoriesRoot category template
版权声明:本文标题:categories - Root category template? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302444a1931534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论