显示所有自定义帖子字段和值,除非为空

时间:2011-07-19 作者:user7126

我希望抓取给定帖子的所有自定义帖子元字段,并将它们显示在页面上,这是我用以下代码完成的。

但是,我想检查(!empty())是否为自定义post元数据,并且不打印任何为空的值。

除此之外,这应该是非常直接的,但我会感谢你的帮助。

<?php
$custom_fields = get_post_custom();
    foreach ( $custom_fields as $field_key => $field_values ) {
        if(!isset($field_values[0])) continue;
        if(in_array($field_key,array("hits","_wp_trash_meta_time","SKU","sub-category-2","sub-category-1","_wp_trash_meta_status","old_price","price","qty","additional_notes","buy_link","customlist1","customlist2","featured","image","images","_edit_lock","_edit_last","_thumbnail_id","_sexybookmarks_permaHash","_sexybookmarks_shortUrl"))) continue;
    foreach ( $field_values as $key => $value )                             
    echo \'<li><strong>\' . $field_key . \':</strong> \' . $value . \'</li>\';
}
?>

1 个回复
SO网友:kaiser

这可以通过默认PHP函数轻松完成。

// Loop through all fields, grab the content and assign it to a new array
foreach ( get_post_custom() as $custom )
    $new_custom[] = array_shift( $custom );

// Get rid of empty fields
$new_custom = array_filter( $new_custom );

结束

相关推荐