Ready to get started?Download WordPress


is_page usage within the loop (4 posts)

  1. yoav.aner
    Posted 2 years ago #

    Just came across some inconsistency. Perhaps it's purely a documentation issue, but I wonder if anybody knows.

    When looking at the docs for is_page() it says that "This tag must be used BEFORE The Loop and does not work inside The Loop".

    However, looking at is_singular() documentation - which seems to be using is_page(), there is no such mention about avoiding using it within the loop.

    Is it ok to use is_singular() and/or is_page() within the loop??

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    I think it depends upon what kind of Loop you are using. It won't work in a standard Posts Loop as it will be permanently false. It will also return true within the Loop on a Page of Posts - which may not be the result you expected. is_singular() will return true for any single Post or Page.

    In general, you'd be better of using if( $post->post_type == 'page' ) within a Loop.

  3. yoav.aner
    Posted 2 years ago #

    Thanks esmi. That was quick! I wonder if the is_page function can perform this test by itself though (I'm guessing it's too complex to be worth it), but anyway this is a very useful tip.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    As I said above, the results can be unpredictable.

Topic Closed

This topic has been closed to new replies.

About this Topic