yoav.aner
Member
Posted 4 months 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??
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
Member
Posted 4 months 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.
As I said above, the results can be unpredictable.