Support » Plugins and Hacks » Hacks » is_page usage within the loop

  • yoav.aner

    (@yoavaner)


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

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    (@esmi)


    Forum Moderator


    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.

    yoav.aner

    (@yoavaner)


    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.

    esmi

    (@esmi)


    Forum Moderator


    As I said above, the results can be unpredictable.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘is_page usage within the loop’ is closed to new replies.