In my loop, I'm trying to set it so that certain text is displayed on certain pages, if there are no posts to display.

I thought this would be simple, using is_post(), but it seems that this function does not work when you are in The Loop.

Does any body know how I can identify the page that is currently being shown, whilst in The Loop?

The below code is from loop.php - it's how I have the code structured, as the content is not important in this instance. However, for those interested, here is my full page.php and loop.php code - http://pastebin.com/hDjR6C7A.

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>

        <!-- Do stuff here -->

    <?php endwhile; ?>

<?php else : ?>

    <?php if(is_page(162)) : // Careers ?>

        <!-- Do stuff here -->

    <?php if(is_page(716)) : // Partnerships ?>

        <!-- Do stuff here -->

    <?php elseif(is_search()) : ?>

        <!-- Do stuff here -->

    <?php endif; ?>

<?php endif; ?>
  • in what template are you using your code? i.e. does your theme have page.php, search.php, and index.php, etc ?
    – Michael
    Commented May 15, 2013 at 12:13
  • is that all the code of your loop or does the template contain a custom query?
    – Michael
    Commented May 15, 2013 at 12:32
  • You appear to be missing an endwhile in your code.
    – Pat J
    Commented May 15, 2013 at 12:48
  • @Patj - Not missing, I just accidentally deleted it for this example code. Added back in now.
    – David Gard
    Commented May 15, 2013 at 13:30
  • @Michael - This in my loop.php file. The Loop is used by various templates to display posts, and in the case of two pages, and the search, I need to show some text if there are not matching posts. I've updated with a link to Pastebin that contains my full code for page.php and loop.php. Thanks.
    – David Gard
    Commented May 15, 2013 at 13:32

2 Answers 2


I believe you want get_queried_object. That will return information about the page per se and not necessarily about the posts/pages being displayed in the Loop.

Be aware that it returns very different information depending on the type of page you are one. It will return user information on an author archive, for example, but category information on a category archive, and sometime it returns NULL.



  • Good idea, but returns NULL in this case. get_queried_object_id() then returns 0. Thanks.
    – David Gard
    Commented May 15, 2013 at 18:09

Try is_single( $page_id ) instead. Basically is_post() has been deprecated.


  • Thanks for the comment, but this is not what I'm looking for. The description even specifically notes that this is not for use for post type 'page'.
    – David Gard
    Commented May 15, 2013 at 13:39

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.