Created
April 16, 2015 07:19
-
-
Save j-cam/1f9255e06345d5eff2b8 to your computer and use it in GitHub Desktop.
Wordpress: Contextually load content template parts for pages via get_template_part().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The template for displaying all pages. | |
* | |
* This is the template that displays all pages by default. | |
* Please note that this is the WordPress construct of pages | |
* and that other 'pages' on your WordPress site will use a | |
* different template. | |
* | |
* Additionally this template will try to retrieve the any | |
* custom template parts created for pages using the convention: | |
* content-{page_name}.php where {page_name} is the slug/title of | |
* the WordPress page you are trying to target. | |
* | |
* @package Foo | |
*/ | |
get_header(); ?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main" role="main"> | |
<?php while ( have_posts() ) : the_post(); ?> | |
<?php | |
// Contextually get the template part. | |
$page_template = locate_template( "content-{$post->post_name}.php" ); | |
if ( $page_template != '' ) { | |
// Yep, load custom template part. | |
get_template_part( "content-{$post->post_name}.php" ); | |
} | |
else { | |
// Nope, load the default content template part (content-page.php). | |
get_template_part( 'content', 'page' ); | |
} | |
?> | |
<?php endwhile; // end of the loop. ?> | |
</main><!-- #main --> | |
</div><!-- #primary --> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment