- This topic has 12 replies, 2 voices, and was last updated 5 years, 2 months ago by
Tom.
-
AuthorPosts
-
April 9, 2020 at 12:38 pm #1233098
Janne
Hi.
Currently the categories are displayed at the end of the post. I want to display them below the title.
Is it possible to display the parent category on the top of the blogpost?April 9, 2020 at 5:57 pm #1233361Tom
Lead DeveloperLead DeveloperHi there,
Are you wanting to display it inline with your other meta items? If so, where would you like them to display? After everything else?
Let me know 🙂
April 10, 2020 at 4:24 am #1233724Janne
We want it below the title of the blogpost. Is it also possible to only show the parent category, not all?
April 10, 2020 at 9:52 am #1234238Tom
Lead DeveloperLead DeveloperWhat if there isn’t a parent?
Should be possible using a Hook Element, just need to figure out the code we’ll need 🙂
April 10, 2020 at 12:43 pm #1234410Janne
If there isnt a parent, we dont want anything to be shown.
April 10, 2020 at 4:47 pm #1234602Tom
Lead DeveloperLead DeveloperYou could try this:
add_action( 'generate_after_entry_title', function() { $cat = get_the_category(); if ( isset( $cat[0]->parent ) ) { var_dump($cat[0]->parent); echo '<div class="parent-category">' . get_cat_name($cat[0]->parent) . '</div>'; } } );
Let me know 🙂
April 11, 2020 at 7:33 am #1235114Janne
Dont lookslike its working. Are now using this code, and that is working. But this displays all categories:
add_action( 'generate_after_entry_title', 'lh_add_cat_below_title' ); function lh_add_cat_below_title() { if (is_single() ) { $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) ); $categories_list = sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', _x( 'Categories', 'Used before category names.', 'generatepress' ), $categories_list ); } ?> <div class="entry-meta"> <?php echo $categories_list; ?> </div> <?php }
Both code snippets are running on my site now.
April 11, 2020 at 7:59 am #1235276Janne
Now I understand what happened, your code added the categories on the blog page, not on the blogpost. It created a mess on my site, but removed your code now.
April 11, 2020 at 4:09 pm #1235682Tom
Lead DeveloperLead DeveloperCan you try the updated code?: https://generatepress.com/forums/topic/display-categories-on-the-top-of-the-blogpost/#post-1234602
April 12, 2020 at 4:33 am #1236040Janne
Nothing happened. Removed my code, and loaded your code, and nothing showed on the blogpost page. Tried running both my and your code, and then all the categories showed. Now both code snippets are active on my site.
April 12, 2020 at 10:00 am #1236421Tom
Lead DeveloperLead DeveloperCan you link me to your site so I can take a look?
April 13, 2020 at 7:17 am #1237282Janne
Have added a link in the first post.
April 13, 2020 at 4:42 pm #1237967Tom
Lead DeveloperLead DeveloperCan you try the updated code?: https://generatepress.com/forums/topic/display-categories-on-the-top-of-the-blogpost/#post-1234602
I added a debugging line in there to see if we have a parent object.
-
AuthorPosts
- You must be logged in to reply to this topic.