WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to grab the current page name and pull that specific template part per page. This way I can just set up a generic tier-1 template that calls in a secondary section based on the current page's title (without having to use a bunch of conditional statements). So, it'd look like this...

<?php get_template_part( 'section', '$pagename' ); ?>

Pulling in the file: section-pagename.php (eg., section-about.php).

Is this possible? I wonder if anyone has used this approach. I think it'd be a really efficient way of templating.

share|improve this question

I see no reason for this to not work technically, only you don't need those single quotes around variable.

But practically why not just use page-{slug}.php or other template option from hierarchy?

share|improve this answer
Ah, you're right. It works without the quotes. Thank you! I appreciate it! I wanted to break up specific secondary areas on pages without creating unique page templates for each of 10-12 pages. Basically, on a magazine-style layout, I want to extend the page past the content entry area. So, for instance, I'll have a page-tier1.php file that defines the inner header and subnav and then generically links to a template_part that pulls in a content area specific to that page. That secondary section pulls in unique content for the 10-12 pages without having to hard-code each call. – swartsr Apr 12 '11 at 17:44
I agree with swartsr: if the only difference between the Pages is what happens in one specific part of the template, then I would go with get_template_part( 'section', $pagename ). If degrades gracefully to section.php if the section-pagename.php doesn't exist, and minimizes duplication of code. – Chip Bennett Apr 12 '11 at 17:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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