• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

What is the "Best Practice" when adding body class to landing page template

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › What is the "Best Practice" when adding body class to landing page template

This topic is: not resolved

Tagged: best practices, landing page template, template, templates

  • This topic has 2 replies, 3 voices, and was last updated 7 years, 10 months ago by Victor Font.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • March 14, 2018 at 10:07 am #217880
    TurboPoet94
    Member

    Hi,

    I'm working with some outside contractors who are insisting the following:

    1) The way that Genesis adds body class to its page_landing.php template is not a "Best Practice" (and subsequently the way I'm adding body classes to multiple LPs is also not a "Best Practice").

    Code from the Genesis News-Pro page_landing.php template the child theme ships with is:

    // Add custom body class to the head
    add_filter( 'body_class', 'news_add_body_class' );
    function news_add_body_class( $classes ) {
    
       $classes[] = 'news-pro-landing';
       return $classes;
       
    }

    They insist that this should be in functions.php and then called using a filter in the template.

    Their functions.php code:

    
    /**
     * Add body foo class
     *
     * @param $classes
     *
     * @return array
     */
    function add_foo_body_class( $classes ) {
    	$classes[] = 'foo';
    	return $classes;
    }

    And then they add this in the template as:

    //* Add custom body class to the head
    add_filter( 'body_class', 'add_foo_body_class' );

    I think they are full of it.

    I also enqueue stylesheets, fonts and scripts that are used ONLY IN Template ABC in the template itself, rather than in functions.php.

    When you have dozens of different landing page templates, as I do, I find that doing everything straight in the template makes more sense - the code is right there.

    If you pull everything out of the individual templates and stick it in functions.php, you end up with an extra 700+ lines of code in functions.php setting up "Here is my body class for Template ABC. Here are my enqueues for Template ABC..." over and over and over.

    What is the general consensus?

    Body class + enqueues that are unique only to Template ABC should be:
    1) kept in Template ABC
    2) all moved to functions.php

    http://private
    March 14, 2018 at 11:46 am #217888
    Erika
    Participant

    Hi Turbo,

    All I can offer is how StudioPress accomplishes adding body classes. In the Magazine Pro theme, they have a landing page template with this code inside it:

    //* Add custom body class to the head
    add_filter( 'body_class', 'magazine_add_body_class' );
    function magazine_add_body_class( $classes ) {
    
       $classes[] = 'magazine-landing';
       return $classes;
       
    }

    They also have a front page template with similar code:

    function magazine_body_class( $classes ) {
    
    	$classes[] = 'magazine-home';
    	return $classes;
    	
    }

    Neither of these templates are called to in the functions.php, so it looks like your line of thinking is on the right track.

    March 14, 2018 at 1:23 pm #217894
    Victor Font
    Moderator

    It really doesn't matter where you add a body class. If it works as you intended, then wherever you placed it is just fine. The developers who told you this is wrong probably have their own definition of best practice.

    In general, what works for me is considering the scope. If I'm adding a body class and it applies to a single template only, then scope is template and that's where the code goes. If I place single-template code in functions.php, then I'd have to edit functions.php for every website where I wanted to use that template. Does that make sense? Placing it in the template means the code travels with the template.

    Now, if scope if broader than a single template, I consider whether the code is for a single site, or if I can use it globally across many sites. I'd choose functions.php for single-site and my custom function library for global use.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble