Tagline in header based on category name

Support Forums for LiveMesh Themes & Plugins Forums Invent Theme Support Tagline in header based on category name

This topic is: resolved

This topic contains 9 replies, has 2 voices, and was last updated by Meteorite meteorite 3 months, 2 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #26724
    Palmtree
    palmtree
    Participant
    Post count: 105

    Hi 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?

    #26741
    Meteorite
    meteorite
    Moderator
    Post count: 3891

    I 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>';
            }
    #26856
    Palmtree
    palmtree
    Participant
    Post count: 105

    This 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)?

    #26872
    Meteorite
    meteorite
    Moderator
    Post count: 3891

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

    #26874
    Palmtree
    palmtree
    Participant
    Post count: 105

    Actually 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-categories

    But it is too technical for me to apply the same principle on to the piece of code you told me.
    Could you help me?

    #26960
    Palmtree
    palmtree
    Participant
    Post count: 105

    It would be really appreciated to look into this…

    #26972
    Meteorite
    meteorite
    Moderator
    Post count: 3891

    Sorry about the delay. Pls give us 24 hours and I will revert to you with a solution. Thank you for your patience.

    #26984
    Meteorite
    meteorite
    Moderator
    Post count: 3891

    I 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">&nbsp;&rarr;&nbsp;</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">&nbsp;&rarr;&nbsp;</span>');
                echo '</h2>';
            }

    and then add this custom CSS –

    #title-area span.category-separator:last-child {
        display: none;
    }

    Hope this works for you.

    #26986
    Palmtree
    palmtree
    Participant
    Post count: 105

    Thanks 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:-)))

    #26996
    Meteorite
    meteorite
    Moderator
    Post count: 3891

    Glad to hear that works well for you. Thanks for letting us know.

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Tagline in header based on category name’ is closed to new replies.