WP Template Parts vs CSS Styles And Scripts
One of our major goals at OnSiteWP is to make WordPress sites easy for businesses to own. Both Mark and I enjoy digging under the hood of websites but you may not hear us “speaking geek” because we understand that our clients have better things to do than listen to us ramble about html, css, php and the like. But just because you don’t hear us spouting jargon doesn’t mean we don’t know how to. While most of our clients like to turn over the keys of their site and know that it’s going to be well taken care of, we also offer consulting services to developers who get in over their head.
I was helping a WordPress designer/developer understand how the parts of a WordPress theme relate to each other. It started as a woocommerce template question but it really applies to all WordPress themes in general.
He was working on a theme file that imported several sections with the WordPress get_template_part() function and was confused about how to apply CSS to his template part file.
get_template_part() is a fancy WordPress function that behaves like a PHP require statement at the end of the day. Once you get past this mental hurdle, the way in which you apply CSS styles and javascript selectors becomes obvious. You don’t write CSS for the template part, you write it for the final HTML output because that is how your web browser sees the page.
For added measure, I got him to pull out his inline javascript snippets and use the WordPress wp_enqueue_script() function for his custom js code. The benefit to using wp_enqueue_script() is proper dependency management.
We will never dive into these sorts of details with 98% of our clients. They want a WordPress site that is reliable, secure, and performant. But don’t be afraid to contact us with your hardest development questions. We are up for the challenge! A couple hours consulting with us is much cheaper than spending weeks of frustration on your own.