Stackexchange用户您好!我构建了一个简单的函数来显示自定义字段中的项目列表。然而,它在前端列出了一个空的最后一个字段,我不知道如何删除它。请帮帮我:)
function something_custom_fields() {
$thumb = get_post_meta($post->ID,\'Thumbnail\', true);
$thumb = ( !empty( $thumb ) ) ? $thumb : get_bloginfo(\'template_directory\').\'/images/icon_something.png\';
$checkmark = get_bloginfo(\'template_directory\').\'/images/icon_check.png\';
$lista = genesis_get_custom_field(\'_something_field_type\', $post->ID);
if(empty($lista)) { return false; }
else {
print \'<div id="something_custom_fields"><img src="\' . $thumb . \'" alt="something" /><ul><h2>List Header</h2>\';
foreach ( $lista as $key => $value ) {
echo \'<li>\' . $value . \'<img src="\' . $checkmark . \'" alt="Post Thumb" class="fitness-checkmark" /></li></ul>\';
if(empty($value) ){
return false; }
}
}
echo \'</div>\';
}
最合适的回答,由SO网友:sander 整理而成
我已经找到了页面上显示的空自定义字段问题的答案。
/** Get Fitness Custom Fields from listing page **/
add_filter( \'woocommerce_single_product_summary\', \'something_custom_fields\', 23 );
function something_custom_fields() {
//Get picture from post meta or template directory
$thumb = get_post_meta($post->ID,\'Thumbnail\', true);
$thumb = ( !empty( $thumb ) ) ? $thumb : get_bloginfo(\'template_directory\').\'/images/icon.png\';
//Get checkmark for after each list item from template directory
$checkmark = get_bloginfo(\'template_directory\').\'/images/check.png\';
//Define custom fields in this case with Genesis
$lista = genesis_get_custom_field(\'_something_field_type\', $post->ID);
// The title of the section
echo \'<div id="style_custom_fields"><img src="\' . $thumb . \'" />
<h2>Title</h2>\';
// the rule to display items in foreach loop
if( $lista ) {
foreach( $lista as $key => $value ) {
// if value is nothing the display none
if( $value != \'\') {
echo \'<li>\' . $value . \'<img src="\' . $checkmark . \'" class="checkmark-style" /></li>\'; }
}
echo \'</div>\';
}
}