if statement in sidebar.php checking page id or template or title

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).

Read More

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!

Related posts

Leave a Reply

3 comments

  1. 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?