Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here is my loop code, i want to call different page templates for different pages. Please notice that this is a one-page-layout.

<?php $args = array( 'post_type' => 'page' );?>  <!-- get only pages -->

<?php query_posts($args); ?>

<?php while (have_posts()) : the_post(); ?>  <!-- loop with pages starts -->

<?php $id = get_the_ID(); ?>  <!-- get post/pageID --> 

<?php   if ($id == 5) : include( get_stylesheet_directory() . '/page_special.php' );

        else: include( get_stylesheet_directory() . '/page_all.php' );

        endif; 
?> 

Would it be possible to change the if ($id == 5) with is_page or sth like that?

share|improve this question
1  
Why do you wrap every single line with php-tags? It's enough to open the php-mode at the beginning and do we it at the end. –  Ralf912 May 5 '13 at 2:42

3 Answers 3

up vote 2 down vote accepted

Yes, there are a few improvements.

  • Needlessly opening and closing PHP tags is a timewaster, makes it harder to read, and means more typing
  • PHP Alt/short style syntax isn't going to play as nicely with the editor. Use {} instead, and your IDE may even help you type out the closing brace automagically.
  • Query_posts, never, ever use it. There is no valid use case that isn't covered by WP_Query or the pre_get_posts filter
  • Dont put a trailing ?> at the end of a PHP file
  • Use PHP comments not HTML comments
  • get_template_part should be used instead of include
  • You made no attempt to do cleanup after your query
  • You never checked to see if any pages were found at all
  • You're using page in your template names, this could cause clashes with the template heirarchy and may not load the template you think in some circumstances

So lets begin:

<?php

// grab pages
$args = array( 'post_type' => 'page' );
$query = new WP_Query( $args );

// did we actually find posts?
if ( $query->have_posts() ) {

    // while there are still posts
    while ( $query->have_posts()) {
        // setup the current post data/globals
        $query->the_post();

        // grab its ID
        $id = get_the_ID();
        if ($id == 5) {
            get_template_part( 'page_special' );
        } else {
            get_template_part( 'page_all' );
        }
    }
    // cleanup after ourselves
    wp_reset_postdata();
} else {
    // no pages were found!
}

Now, you can use get_template_part to further extend this.

First, I would swap this out to use 'content' rather than 'page' as the main identifier. So:

  • page_all.php becomes content.php
  • page_special.php becomes content-special.php

Note the use of - not _.

Then, I would modify the code that loads the content template, like so:

get_template_part( 'content', get_the_ID() );

If you land on page 4 for example, it will attempt to load content-4.php, and if that doesn't exist, it will load content.php. You can pass in anything you want as the second parameter, and it'll also look in child/parent themes. Tie this to a custom field/post meta and you can extend it further

share|improve this answer
    
Hi Tom! Thank you very much for your detailed and commented solution. I like the get_template_part( 'content', get_the_ID() ); part because i think this is what i was lookin for. You might have noticed that i'm a PHP noob but you gave me very good starting advices. Thank you. –  mobsteady May 5 '13 at 8:00
    
Glad to have helped, if you're happy with it you could mark it as accepted? I'd say the most important note to stress is query_posts, it's used in so many tutorials yet it's can cause all manner of issues, and it essentially doubles the amount of work WordPress has to do –  Tom J Nowell May 5 '13 at 12:15

First of all, don't use query_posts. Check this question for more info.

Secondly, post ID is probably not a good thing to differentiate on: duplicating database tables can easily become a pain in case of multiple-server development flows (i.e. one for dev, optionally another for QA, and one for live).

I found it the least brittle to tie logic to a custom field or something similar, such as a plugin setting which allows the user to specify the page ID(s) to show a different template for.

share|improve this answer
    
hey dalbaeb! thank you for the link. I hope i will get into the "query thing" soon :) –  mobsteady May 5 '13 at 8:02

I love ternary operators

$part = ( 5 === (int) get_the_ID() ) ?
  'page_special' : 'page_all';
get_template_part( $part );
share|improve this answer

Your Answer

 
discard

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.