Community Forums › Forums › Archived Forums › General Discussion › What is the "Best Practice" when adding body class to landing page template
Tagged: best practices, landing page template, template, templates
- This topic has 2 replies, 3 voices, and was last updated 7 years, 1 month ago by
Victor Font.
-
AuthorPosts
-
March 14, 2018 at 10:07 am #217880
TurboPoet94
MemberHi,
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:
http://private
1) kept in Template ABC
2) all moved to functions.phpMarch 14, 2018 at 11:46 am #217888Erika
ParticipantHi 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 #217894Victor Font
ModeratorIt 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? -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.