admin管理员组文章数量:1391975
I'm very new to coding so please bear with me. I am trying to show the categories of which my post came from on the post excerpt. I have added a photo to demonstrate what I mean:
Here, the post "surgical sightings" shows the parent category "Education" as well as the 'sub-categories' under Education. I was wondering if there was a function that allows me to do this? Right now my post only has the title of the post, the date and an excerpt like this. Ideally I could add the categories under which the post is under as well.
I have spent an hour or so looking up php and css and have no idea how to edit these on my Wordpress site. Any help would be much appreciated thank you!
Edit: Picture showing drop-down categories on menu.
Edit #2: Folder of my theme's php folder structure (Nisarg)
I'm very new to coding so please bear with me. I am trying to show the categories of which my post came from on the post excerpt. I have added a photo to demonstrate what I mean:
Here, the post "surgical sightings" shows the parent category "Education" as well as the 'sub-categories' under Education. I was wondering if there was a function that allows me to do this? Right now my post only has the title of the post, the date and an excerpt like this. Ideally I could add the categories under which the post is under as well.
I have spent an hour or so looking up php and css and have no idea how to edit these on my Wordpress site. Any help would be much appreciated thank you!
Edit: Picture showing drop-down categories on menu.
Edit #2: Folder of my theme's php folder structure (Nisarg)
Share Improve this question edited Feb 28, 2020 at 13:25 Eric asked Feb 23, 2020 at 13:28 EricEric 153 bronze badges 2- What are "Med Jurnal", "Medicine" & "year 2" ? means is it parent categories like Education OR sub categories of "Education" ?? So I can help you. – Tanmay Patel Commented Feb 24, 2020 at 4:10
- Hi thanks for your response, yes 'medicine', 'med journal' and 'year 2' are sub-categories under the parent category Education and sub-categories within each other which is on the primary menu. I have added a photo for clarification. – Eric Commented Feb 24, 2020 at 20:45
2 Answers
Reset to default 0Use the_category();
on your template. It also supports $separator
parameter so you can use your desired separator.
Example: <?php the_category( ', ' ); ?>
will separte categories with commas.
By the look of your theme folder structure, you should add <?php the_category( ', ' ); ?>
on child-theme
=>template-parts
=>content-excerpt.php
. Add it to line 22. If you want it only for the excerpt posts, use it on line 26.
Edit: So to answer the question directly.. The category for every post is actually an array. If you want to get just one (the 'first' one), you can do:
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}
the provided code by Bikram is correct and works if you want to get all of them.
Original Comment and further clarifications: First of all, what theme are you using? If the theme was not developed by yourself you should be editing things in a child theme that you create (or ideally is supplied by the former parent theme's developer).
With that said, you may want to look into Wordpress' template hierarchy and how Wordpress theming works.
I am guessing from your question that you're trying to get this done in the listing page. Depending on the theme you're using that may be the index.php in the root folder of your theme, but might as well be coming from archive.php, search, category or even a custom template from the theme.. It really depends on the theme's structure. Maybe if you share a screenshot of your theme's folder structure we could try and help you a bit further.
The 404.php, for example, is only used to show the 404 page. This is basically a "Not Found" page, not sure why you are trying to get the provided code by Bikram in there.
本文标签: phpTrying to show the category of a post in the post display
版权声明:本文标题:php - Trying to show the category of a post in the post display 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744704511a2620760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论