Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialElla Ruokokoski
20,881 PointsWordpress Problem: Loop through your custom page templates and add their content to the single page layout
Hey!
I am developing a custom Wordpress theme, that is supposed to be a single page theme, and I am facing problem when trying to get my custom page template content to display on my site.
I have read the documentation and tried stuff out but I don't seem to get it work. I have created a couple of Wordpress themes before, and also created a single page theme before, but that didn't use custom pages. I am not a Wordpress expert and am still not familiar with a lot of the functions.
My situation right now is that I have a loop in my page.php that goes through the pages and posts each page's content to a section element so the content is displayed on a single page. This content however is only the content that I have written in the editor when editing the page and not the custom content from my page-cv.php or my page-music.php files. I am able to display the custom content with <? php get_template_part() ?> but the page then has duplicate sections of my music and cv sections since my loop already displays the content from the page editor.
so is there a way to loop through all the pages and somehow check if a template is a custom template and show its content and if not, show the "default" content.
I would really appreciate help!
2 Answers
Ella Ruokokoski
20,881 PointsI actually figured this out after all with my oh so fresh morning brain. Pretty easy solution, I can't believe it took me this long to figure it out
here's my page.php code
<?php
get_header();
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'hierarchical' => 1,
'exclude' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach ($pages as $page_data) {
$content = apply_filters('content', $page_data->post_content);
$slug = $page_data->post_name;
?>
<section>
<?php
if($slug == "cv") { ?>
<div>
<?php echo $content;?>
</div>
<?php
get_template_part("page", "cv");
}
if($slug == "music") {
?><div>
<?php echo $content;?></div>
<?php
get_template_part("page", "music");
}else{
?>
<?php echo $content; ?>
<?php } ?>
</section>
<?php
}
?>
<?php get_footer(); ?>
I don't have get_header or get_footer in my page templates so they will not get posted on my site.
Brais Cao
910 PointsYou can simply do get_template_part('page', $slug); This way you dont need the if statement and the code is cleaner and less repetitive :)