GET_POST-检查自定义域是否有内容?

时间:2017-12-21 作者:Henning Fischer

我有一个很好的循环,但我必须检查post对象(“houses”)的自定义字段是否有内容。我用“isset”试了一下,什么都没发生。也试过“isset”,但问题是一样的。

$args2 = array (
    \'numberposts\' => -1,
    \'post_type\'   => \'houses\'
);
$custom_posts2 = get_posts($args2);
foreach ( $custom_posts2 as $custom_post2 ) {

// check here if custom post type opjects custom field "sz_website" has content
if ( !empty ($custom_post2->post_website)) {
    echo \'<div class="wpcf7-list-item-field">\' . $custom_post2->sz_website . \'</div>\';
} else { 
    echo "do nothing"; 
}

}
wp_reset_postdata();

1 个回复
最合适的回答,由SO网友:Sid 整理而成

尝试以下操作:

$args2 = array (
    \'numberposts\' => -1,
    \'post_type\'   => \'houses\'
);
$custom_posts2 = get_posts($args2);
foreach ( $custom_posts2 as $custom_post2 ) {

// check here if custom post type opjects custom field "sz_website" has content
$sz_website = get_post_meta( $custom_post2->ID, \'sz_website\', true );

    if ( !empty ($sz_website)) {
        echo \'<div class="wpcf7-list-item-field">\' . $sz_website . \'</div>\';
    } else { 
        echo "do nothing"; 
    }

}
wp_reset_postdata();
如果有帮助,请告诉我。

结束

相关推荐