Support Forums for LiveMesh Themes & Plugins › Forums › Invent Theme Support › Tagline in header based on category name
- This topic has 9 replies, 2 voices, and was last updated 7 years, 8 months ago by
Raghavendra.
-
AuthorPosts
-
May 24, 2017 at 5:18 pm #26724
palmtree
ParticipantHi Code Master,
I’ve found that the solution you have suggested isn’t working well
https://support.livemeshthemes.com/forums/topic/change-tagline-in-header-title-area-based-on-category/I thought by doing this
elseif (is_singular('post')) { $categories = get_the_category(); echo '<h2>'; foreach ($categories as $cat) { echo get_category_parents($cat, true, ' * '); } echo '</h2>'; }
could solve but it is only good for singular level (single post). When it comes to category level,it becomes strange.
I have a single post title “beautiful day” under category “A” and The category “A” belongs to “X” category.
E.g. X (parent category) > A (child category) > beautiful day (single post under cat A).When I am viewing single post “beautiful day”, above code outputs tagline
X * A *
So this is good.Now, when I go to category “A”, the tagline prints
XA
No space or * mark in between.Could you help me with this?
May 29, 2017 at 2:07 pm #26741Raghavendra
ModeratorI guess you are just looking for this piece of code with separator added to the category check –
elseif (is_singular('post')) { $categories = get_the_category(); echo '<h2>'; foreach ($categories as $cat) { echo get_category_parents($cat, true, ' * '); } echo '</h2>'; } elseif (is_archive() || is_category()) { $cat = get_category( get_queried_object_id()); echo '<h2>'; echo get_category_parents($cat, true, ' * '); echo '</h2>'; }
June 16, 2017 at 1:27 am #26856palmtree
ParticipantThis is really getting closer.
Your suggestion is great when looking at Child Category or Single post in that child category. It prints below.
Parent Name * Child Name *
So it looks good but… When clicking Parent category, the parent name outputs with “*” asterisk like below.
Parent Name *
Is there any solution to get rid of this asterisk when only parent name is output (when viewing parent category)?
June 19, 2017 at 1:55 pm #26872Raghavendra
ModeratorPls search for the text ‘get_category_parents remove last separator’ in Google and you should find some suggestions. Pls do let me know if you have trouble getting this to work. Thank you.
June 20, 2017 at 3:37 am #26874palmtree
ParticipantActually there weren’t many google result when I typed as you have suggested…
The closest one was
https://wordpress.stackexchange.com/questions/646/wordpress-remove-br-separator-from-last-item-in-wp-list-categoriesBut it is too technical for me to apply the same principle on to the piece of code you told me.
Could you help me?July 4, 2017 at 2:49 am #26960palmtree
ParticipantIt would be really appreciated to look into this…
July 6, 2017 at 1:53 pm #26972Raghavendra
ModeratorSorry about the delay. Pls give us 24 hours and I will revert to you with a solution. Thank you for your patience.
July 7, 2017 at 6:30 am #26984Raghavendra
ModeratorI did not take a look at the PHP solution. Instead, thought of an easier CSS solution as provided below.
Pls change the above custom code for categories to –
elseif (is_singular('post')) { $categories = get_the_category(); echo '<h2>'; foreach ($categories as $cat) { echo get_category_parents($cat, true, '<span class="category-separator"> → </span>'); } echo '</h2>'; } elseif (is_archive() || is_category()) { $cat = get_category( get_queried_object_id()); echo '<h2>'; echo get_category_parents($cat, true, '<span class="category-separator"> → </span>'); echo '</h2>'; }
and then add this custom CSS –
#title-area span.category-separator:last-child { display: none; }
Hope this works for you.
July 7, 2017 at 8:33 am #26986palmtree
ParticipantThanks for your amazing support!
I didn’t think about CSS approach. Your way of thinking outside of the box is so smart!!
I like the little arrow too:-)))July 10, 2017 at 11:01 am #26996Raghavendra
ModeratorGlad to hear that works well for you. Thanks for letting us know.
-
AuthorPosts
- The topic ‘Tagline in header based on category name’ is closed to new replies.