WordPress.org

Support

Support » Themes and Templates » How to add an additional class to headlines

How to add an additional class to headlines

  • This is not theme specific, but for the sake of argument lets say we are talking about the Genesis theme because my query is about child themes.

    Lets say I have written a custom CSS class by the name “headline-wrap” and want to wrap my h2 headlines (post titles) with this new class. Since I want this to be strictly a child theme, I don’t want to edit the template structure file manually. Maybe its something I have to add in the functions.php

    Can someone assist me with this?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Andrew Nevins

    @anevins

    Forum moderator

    Perhaps jQuery’s addClass function?
    http://api.jquery.com/addClass/

    No its certainly not what I am looking for. Its not that complex. It is something about adding a filter and an echo

    filter (fn_add_headline)
    <h2 <?php echo class=”headline-wrap”/>>

    Something like that. I searched everywhere but could not get the syntax right. If someone could please type that line for me so that I can paste it in my functions.php that would be great.

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    Strings in PHP are wrapped in either apostrophes or speech marks.
    PHP ends with the ?> tag.

    WPyogi

    @wpyogi

    Forum Moderator

    Why not just hardcode the class into the h2 tags in the header.php — but in a modified copy of that file in the child theme?

    @wpyogi Thanks. Lets call that a workaround but still for the sake of learning, if I had to do it by using filters in the functions file, what would I have to type?

    Sometimes using filters would be infinitely easier than the hardcoding part because different themes are coded differently, for example the Genesis theme has no mention of h2 tags in the header template because it calls another function, wrap and inner, so that leads me to chase those functions. If I had the filter code then I could just paste it in the functions.php and the extra class would be appended to the element of my choice regardless of how the template was coded.

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    Hardcoding the class would be something like;

    <h2 class="headline-wrap">

    Instead of

    <h2 <?php echo class="headline-wrap"/>>

    Moderator cubecolour

    @numeeja

    [ Previous post by ginascott removed for not adhering to forum guidelines ]
    ginascott please keep your participation on these forums civil. Insulting someone who is pitching in to help is not appropriate. Everyone posting here is a volunteer. If you disagree with an answer given or you think someone has misunderstood what you are asking for, you can discuss the subject politely, or if that is too hard for you maybe just ignore the post.

    Posting disparaging comments towards other forum members is not acceptable.

    Moderator keesiemeijer

    @keesiemeijer

    If I had the filter code then I could just paste it in the functions.php and the extra class would be appended to the element of my choice regardless of how the template was coded.

    That’s not how filters work. You can only add a filter to elements if the theme/plugin applied a filter for that element. There is no such thing as adding a filter (to html elements) that would work on all themes.

    That’s why you create a child theme to make the changes you want. Or add the class with javascript as Andrew Nevins mentioned.

    Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    Unfortunately, Genesis isn’t freely available, so I can’t actually look at it and tell you how to do it.

    But, if you assume that the theme essentially contains this:

    <h2>

    Then there’s no way to add a filter to that after the fact short of using Javascript.

    If the theme includes the h2 in some other way, then perhaps, but the essentials of how the base framework works is important to your answer. There is no one-size-fits-all solution.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to add an additional class to headlines’ is closed to new replies.