2

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.

1 Answer 1

2

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?

2
  • 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
    Commented Apr 12, 2011 at 17:44
  • 2
    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. Commented Apr 12, 2011 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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