-1

I'm writing a custom theme. Most of the content will use the regular index.php, header.php and footer.php.

The index.php looks like

<?php get_header(); get_sidebar(); ?>
<div id="content">
<?php
while ( have_posts() ) {
    the_post();
    the_content();
} // end while
?>
</div>
<?php get_footer(); ?>

But there will be some pages with special content, so I need to write custom templates php files that I assign to some pages. Now, in order to embed this special content within the main frame of the website, I have to do

<?php get_header(); get_sidebar(); ?>
<div id="content">
<?php
// [special template php code]
?>
</div>
<?php get_footer(); ?>

in every custom template. As you can see, this is the exact the same, just with the main loop replaced by custom content. I'd like to avoid code duplication as much as possible. So, that in my template I only have to put [special template php code] instead of the whole framework.

Is there convenient way to use the default frame that is defined in index.php (header, footer, sidebar, etc.) without having to call the functions in every template again and again? I thought about some php switch in index.php, but this has to be maintained as well (if page id changes for example, if I use page id's as the switch variable). Or if I intend to do changes on the <div id="content"> I would have to do it in every template as well. I'd like to avoid this.

  • I think @Rafawhs has answered the question appropriately but if you still have some doubts, then see this to lean the entire procedure profoundly regarding custom page creation. I am sure it will help you out to address the mistakes you are doing. – user75224 Jun 25 '15 at 12:47
0

I think you are doing this right... but in the wrong direction. What if you separate your default structure like:

content-header.php

<?php get_header(); get_sidebar(); ?>
<div id="content">

content-footer.php

</div>
<?php get_footer(); ?>

And then you can use get_template_part to put all together:

index.php

<?php get_template_part( 'content', 'header' ); ?>

<?php
while ( have_posts() ) {
    the_post();
    the_content();
} // end while
?>

<?php get_template_part( 'content', 'footer' ); ?>

page-template.php

<?php get_template_part( 'content', 'header' ); ?>

// [special template php code]

<?php get_template_part( 'content', 'footer' ); ?>

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.