0

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:

enter image description here

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.

enter image description here

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.

enter image description here

Edit #2: Folder of my theme's php folder structure (Nisarg)

enter image description here

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 Feb 24 '20 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 Feb 24 '20 at 20:45
0

Use 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.

8
  • Hi thanks for your response. Where exactly would I be putting this code in? I tried putting <?php the_category( ', ' ); ?> into the 404.php and single.php but it doesn't seem to be making a difference. – Eric Feb 25 '20 at 9:28
  • @Eric - It has to be inside the loop, so 404.php wont work. Look for <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> line on your single.php. The loop ends with <?php endwhile; ?>. So putting anywhere in between those lines of codes will work. – Bikram Feb 26 '20 at 2:47
  • @Eric - Are you editing your child theme? If not create a child theme and edit your single.php on the child theme. It will save your edits, while updating your theme. – Bikram Feb 26 '20 at 2:48
  • I've found the line you are referring to in the search.php folder but when I put in <?php the_category( ', ' ); ?> inside it there is no change. Would it be because my 'parent' categories for the drop down are actually custom links? I heard this was easier to use – Eric Feb 28 '20 at 13:46
  • @Eric - Your need is still unclear. I thought you wanted to show categories on single.php but I think you want it on the archive or on the search result. Please provide images of the same post so that we understand clear. Also, I have checked the theme for you. If you clearly explain your needs, where do you want to show the categories, I might be able to help. – Bikram Feb 28 '20 at 14:44
0

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.

2
  • Hi, thanks for your response! The theme I'm using is Nisarg, I've created a child theme using the Child Theme Configurator plugin but using the <?php the_category( ', ' ); ?> code isn't working for me. I have edited my original post to show Nisarg's folder structure. Thanks for your patience. – Eric Feb 28 '20 at 13:23
  • Can you expand on where are you trying that <?php the_category( ', ' ); ?> code at? You want to be doing that in the single.php file, or "Single Post" in your plugin's menu item near the bottom of that last screenshot you shared.. This needs to be placed within "the loop", which basically gets the markup content rendered for each post and is a while loop. Edit: nevermind.. Seems like it been solved in the comments. Edit 2: Consider marking the answer that helped you get this solved as the accepted answer. – Cato Mar 12 '20 at 8:53

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.