What to do when “is_page()” does not work

Why “is_page()” doesn’t work

When you want to target a specific page or category using conditional branching in WordPress, you use conditional tags such as is_page() and is_category(). However, in some situations, especially after a query has been customized, these conditional tags may not work correctly.

Solution: wp_reset_query();.

wp_reset_query(); can be used to reset the main WordPress query. This solves the problem of condition tags not working properly, e.g. after a custom loop.

<?php wp_reset_query(); if(is_page(array('top','qa')) || is_category('news')) : ? >
<?php endif; ? >

Notes

It is recommended that wp_reset_query(); be used after the loop and before any conditional tags or other query-related functions. Also note that using this function more than necessary may cause unexpected behavior.

Conclusion

If condition tags do not work as expected, using wp_reset_query(); to reset the main query can solve the problem. However, be sure to use it in the appropriate place.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top