为什么这个NULL结果匹配我的if语句


Why is this NULL result matching my if statement?

这是我的if语句。很简单:

    <?php if ( is_page( 'Level 1 Promo' ) || is_page( 'Order Thank You' ) ) : ?>
      <p style="margin-top:0;">ClickBank is the retailer of products on this site. CLICKBANK ® is a registered trademark of Click Sales, Inc., a Delaware corporation located at 917 S. Lusk Street, Suite 200, Boise Idaho, 83706, USA and used by permission. ClickBank's role as retailer does not constitute an endorsement, approval or review of these products or any claim, statement or opinion used in promotion of these products.</p>
    <?php else : ?>
      <?php foundationPress_footer_nav(); ?>
    <?php endif; ?>

这是我测试is_page()所做的:

<?php global $post;
$pagename = $post->post_name;
echo 'page name: ' . $pagename; ?>

所以一切正常。但是当$pagename为NULL时,第一个if语句通过。

我不知道为什么。出了什么问题,解决办法是什么?

如果你阅读文档,你会看到:

传递空值返回TRUE如果有可能传递空值作为参数来检查特定页面,请非常小心,因为以下几行将返回true:Is_page (")
Is_page (0)
Is_page ('0')
Is_page (null)is_page (false)
Is_page (array())

linus是正确的。

除此之外,您可以为empty($pagename)添加额外的检查。

你可以这样重写你的if。

if (is_page(array( 'Level 1 Promo', 'Order Thank You'))  && !empty($pagename)) {
// Returns true when the Page displayed is either Level 1 Promo or Order Thank You