I would like to use get_template_part(); to load a different template parts on different pages. so on the home page it loads get_template_part( 'content', 'home' ); and on the blog page it would load get_template_part( 'content', 'blog' );.

I'm new to PHP but should there be a conditional tag that I could use to load things by page?

  • When you say a conditional tag, do you mean something like these Conditional Tags?
    – Pat J
    May 20 '15 at 20:55
  • You're thinking xml tags, think php variables, get_template_part('content', $page ); May 20 '15 at 22:24

For question as stated it would be easiest to retrieve page's slug and pass it as second argument:

get_template_part( 'content', get_post_field( 'post_name', get_post() ) );

Note that you would want to have generic content.php for pages that don't have dedicated template (if any).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.