WordPress.org

Ready to get started?Download WordPress

Forums

How to add an additional class to headlines (10 posts)

  1. ginascott
    Member
    Posted 1 year ago #

    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?

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

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

  3. ginascott
    Member
    Posted 1 year ago #

    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.

  4. Andrew
    Forum Moderator
    Posted 1 year ago #

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

  5. WPyogi
    Volunteer Moderator
    Posted 1 year ago #

    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?

  6. ginascott
    Member
    Posted 1 year ago #

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

  7. Andrew
    Forum Moderator
    Posted 1 year ago #

    Hardcoding the class would be something like;

    <h2 class="headline-wrap">

    Instead of

    <h2 <?php echo class="headline-wrap"/>>
  8. cubecolour
    ɹoʇɐɹǝpoɯ
    Posted 1 year ago #

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

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic