1

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!

5
  • 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
    Commented Sep 9, 2011 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
    Commented Sep 10, 2011 at 0:04
  • This is going to be really difficult for us to diagnose if it only exists on a local development environment. Commented Sep 10, 2011 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
    Commented Sep 10, 2011 at 10:52
  • Can you post a pastebin of the todaysmenu-page.php template file? Commented Sep 10, 2011 at 13:39

3 Answers 3

0

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 }
1
  • 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
    Commented Sep 10, 2011 at 0:02
0

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?

4
  • Thanks - answer to both is 'yes'. No, I don't have a live link; the site is only local atm.
    – circey
    Commented Sep 10, 2011 at 0:04
  • See answer edit. Commented Sep 10, 2011 at 1:37
  • If I rename todaysmenu-page.php, the page then reverts to page.php template.
    – circey
    Commented Sep 10, 2011 at 10:49
  • Try resetting the page to use the Default template, and then try to apply the todaysmenu-page.php template? Commented Sep 10, 2011 at 13:38
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.