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.

I have the following code in my sidebar.php file:

<ul class="list-posts">

    <?php
        $lastposts = get_posts('numberposts=5&orderby=date&cat=-52');
        foreach($lastposts as $post) :
        setup_postdata($post); 
        $page_name = $wp_query->post->post_name;

        if (is_page_template('news-page.php')) { ?>
        <p>yes!</p> 
        <?php } else { ?>
        <li<?php if ( $post->ID == $wp_query->post->ID ) { 
       echo ' class="current"'; } else {} ?>>
            <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
        </li>         
         <?php }  ?>
    <?php endforeach; ?>
</ul>

The aim is that if the sidebar appears in any page except the template news-page.php, it executes the code listing all posts within the category. If not, it prints <p>yes!</p> (this code will change - it's just a placeholder atm).

To be honest, I'm not married to the is_page_template, but I've tried is_page, the_title and wp_title, none of which seem to output <p>yes</p>.

Am I going about this the wrong way? Is it not working because it's looking for the page template within the sidebar.php?

I'm pretty new to WP, so specific advice is much appreciated! MTIA.


EDIT:

Ok, this is really weird. I added 'get_page_template' to my code so I could see exactly which template it's using and it's not using news-page.php even though I've specified this in the admin area.

I've created a new page and have also used news-page.php as the template. Again, when I view the page it does not use news-page.php as the template.

In both these instances, the template that is being used is 'todaysmenu-page.php'. I have checked in both files that the correct template name is specified. They are:

<?php
/*
Template Name: Todays menu page
*/
?>

and

<?php
/*
Template Name: News page
*/
?>

Have I completely missed something? Do I need to flush a db or something?


EDIT no. 2:

So the page is definitely using news-page.php, however, the sidebar.php is still outputting that the template being used is todaysmenu-page.php.

So why the heck isn't is_page_template spitting out the correct template?


EDIT no. 3:

Answer: I put the query_posts in the wrong place. Not working code:

<?php get_header(); ?>

      <?php
         query_posts('cat=4');
         if (have_posts()) : while (have_posts()) : the_post();
      ?>

  <div id="content-wrap">
    <div id="side-column"><?php get_sidebar(); ?></div>
    <div id="content-column">
      <h2>News</h2>
      <article>             
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
         <div class="entry-content">
           <?php the_excerpt(); ?>
           <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>      
         </div>
      </article>
      <?php endwhile; endif; ?>
    </div><!--content column-->
  </div><!--content wrap-->
<?php get_footer(); ?>

Working code:

<?php get_header(); ?>
  <div id="content-wrap">
    <div id="side-column"><?php get_sidebar(); ?></div>
    <div id="content-column">
      <h2>News</h2> 

      <?php
         query_posts('cat=4');
         if (have_posts()) : while (have_posts()) : the_post();
      ?>

      <article>             
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
         <div class="entry-content">
           <?php the_excerpt(); ?>
           <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>      
         </div>
      </article>
      <?php endwhile; endif; ?>
    </div><!--content column-->
  </div><!--content wrap-->
<?php get_footer(); ?>

Thanks all for your suggestions and comments!

share|improve this question
    
do you need the 'yes!' to be within the ul tags and be repeated for each of the last posts? what if you move the conditional tag to before your 'get_posts' code? –  Michael Sep 9 '11 at 8:58
    
Yes, I do need the 'yes' to be within the ul tags. This will eventually be replaced with <li><a href""> etc once the code is working. –  circey Sep 10 '11 at 0:04
    
This is going to be really difficult for us to diagnose if it only exists on a local development environment. –  Chip Bennett Sep 10 '11 at 1:32
    
Yeah, I appreciate that. Apologies, but I don't yet have the client's hosting details and I'm loathe to upload the entire site for testing (which I should be able to do locally) and then shift it to the client's hosting when available. If I can't sort this out locally then I'll have to look at uploading to my own hosting temporarily. –  circey Sep 10 '11 at 10:52
    
Can you post a pastebin of the todaysmenu-page.php template file? –  Chip Bennett Sep 10 '11 at 13:39

3 Answers 3

Simplify your code for a test

if (is_page_template('news-page.php')) { ?>
    <p>yes!</p> 
    <?php } else { ?>
    <p>not 'news-page.php'</p>         
     <?php }
share|improve this answer
    
Thanks - I should've tried this to begin with. It's definitely not seeing that the page is using the news-page.php template. I checked in the admin area that the page is using the news-page.php template and it absolutely is. –  circey Sep 10 '11 at 0:02

Two things to check:

  1. The page template is actually named page-news.php.
  2. The current page actually has the page-next.php template assigned.

Do you happen to have a live link?

EDIT

Okay, so for some reason, the Page is using the todaysmenu-page.php template file. That's strange.

What happens if you rename todaysmenu-page.php as todaysmenu-page.php.old? What template file does the Page use now?

share|improve this answer
    
Thanks - answer to both is 'yes'. No, I don't have a live link; the site is only local atm. –  circey Sep 10 '11 at 0:04
    
See answer edit. –  Chip Bennett Sep 10 '11 at 1:37
    
If I rename todaysmenu-page.php, the page then reverts to page.php template. –  circey Sep 10 '11 at 10:49
    
Try resetting the page to use the Default template, and then try to apply the todaysmenu-page.php template? –  Chip Bennett Sep 10 '11 at 13:38
up vote 0 down vote accepted

Okay, turns out my query_posts code in news-page.php was in the wrong place. See my updated question (Edit no. 3).

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.