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

if (is_single()) {
    echo '1';

} elseif ( is_page() ) {
    echo '2';

} else {
   echo '3';
}

?>

I put this snippet at the top of index.php, which is what I'm using to govern the layout of my blog post feeds. I then created a new page, set template to default, and left it blank. It shows all of my posts. But the snippet above echoes a 3. It's using index.php as the template, but it's a page. Why isn't it echoing 2?

UPDATE

Since the blog feed page was set in Settings > Reading, I had to use the conditional is_home();. Apologies for all of the confusion.

share|improve this question

2 Answers 2

Conditional tags mostly derive from and rely on main query.

  • is_page() function calls
    • $wp_query->is_page() method
      • which checks $wp_query->is_page property (set during load) and some more stuff if you asked to check for specific page.

So the likely reason for conditional tag not to produce expected return is issue with global $wp_query, for example it getting erased and overwritten with query_posts() call.

share|improve this answer
    
To that effect, you may wish to echo get_the_ID(); and see if it matches with the ID that appears in the link when you hover over the Edit Page(Post) link. –  GhostToast Jun 18 '13 at 18:00
    
@GhostToast - echo get_the_ID(); was blank. <?php the_ID(); ?> displayed the post number of the first post that shows up in the feed, but not the ID of the feed page itself. –  user1729506 Jun 18 '13 at 18:09

Expanding on @Rarst answer, you will need to run a wp_reset_query() or wp_reset_postdata() at the end of your loop (feed). This will set the global variable $wp_query back to what it should be, and your conditionals should work as intended.

share|improve this answer
    
Tried both. It's still echoing a 3. –  user1729506 Jun 18 '13 at 18:13
1  
Show us your full code please. –  GhostToast Jun 18 '13 at 18:14
    
Just a standard post-displaying page –  user1729506 Jun 18 '13 at 18:20
    
If that were true, you wouldn't be here –  GhostToast Jun 18 '13 at 18:22
    
I assure you, it is. Just a normal page that gets posts, calls the header and footer, and not much else. I updated my original post above to explain why I was having this problem. It was an oversight on my part, because of a setting in Settings > Reading. –  user1729506 Jun 18 '13 at 18:30

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.