Add a class on only one page using "before_post" hook

Community Forums Forums Design Tips and Tricks Add a class on only one page using "before_post" hook

This topic is: not resolved

This topic contains 7 replies, has 2 voices, and was last updated by  jorwesflow 11 months, 1 week ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #40607

    jorwesflow
    Participant
    Post count: 17

    As the title states, I’d like to add a class for use on only one page (id is ’2′) that’s placed in the genesis_before_post hook. I tried making something up to put in the child’s functions.php file, but it didn’t work of course. It looked like this:

     
    `
    add_action( ‘genesis_before_post’ );
    function add_body_class( $classes ) {
    if ( is_page( ’2′ ))
    $classes[] = ‘blog-page-title’;
    return $classes;
    }
    `

    http://projectsacramento.com/
    #40623

    jorwesflow
    Participant
    Post count: 17

    Just to clarify, what I’m trying to do is put a title graphic image on the blog page above all the posts. I would use CSS to define that image as a background.

    • This reply was modified 11 months, 1 week ago by  jorwesflow.
    • This reply was modified 11 months, 1 week ago by  jorwesflow.
    #40629

    braddalton
    Participant
    Post count: 7442

    Better to use PHP and generate the new div class to style your image using a custom function with conditional tag for the blog page.

    Change the Hook if needed

    Or create a custom widget area in the hook position before posts on the blog page.

    Or simply use a custom blog template and then add your image using the WordPress editor.


    WordPress Developer & Consultant
    Brad Dalton400+ StudioPress Tutorials & 1200+ Code snippets

    #40705

    jorwesflow
    Participant
    Post count: 17

    Brad, thanks for the thorough response! I tried the custom function and it didn’t do anything. I of course changed the hook to ‘genesis_before_post’. I tried switching ‘blog’ with the page id which is ’2′ and still nothing.

    Again, all I want to do is generate a new div class on the blog page that hooks on ‘genesis_before_post’

    #40714

    braddalton
    Participant
    Post count: 7442

    I just tested that code again and updated that post so I know it works. Not sure why it doesn’t on your site.

    Please paste the code here using opening and closing[ php ] shortcodes so I can take a look.

    Try the genesis_before_loop hook


    WordPress Developer & Consultant
    Brad Dalton400+ StudioPress Tutorials & 1200+ Code snippets

    #40750

    jorwesflow
    Participant
    Post count: 17

    Brad, worked perfect! The mistake I was making was using the hook ‘genesis_before_post’ instead of your suggested ‘genesis_before_loop’. I’m not savvy enough to know why, but my guess is that it has something to do with the Blog page containing multiple posts? I should have tried some other hooks and gone with ‘before_loop’ from the start!

    Also, using the page id ’2′ instead of ‘blog’ was a deal breaker as well.

    Thanks again for your time.

    #40753

    braddalton
    Participant
    Post count: 7442

    Yes. Made it myself when testing the code.

    Also, using the page id ’2′ instead of ‘blog’ was a deal breaker as well.

    Really, So blog worked?


    WordPress Developer & Consultant
    Brad Dalton400+ StudioPress Tutorials & 1200+ Code snippets

    #40776

    jorwesflow
    Participant
    Post count: 17

    No, what I meant was ‘blog’ didn’t work, but ’2′ did.

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

You must be logged in to reply to this topic.