如果存在组字段元数据+容器div,如果字段为空,如何显示默认文本?[CMB2]

KXX*_*XXT 5 php arrays foreach metadata is-empty

我不是程序员,所以我对解决方案毫无头绪.我一直在使用CMB2 作为投资组合/项目自定义帖子类型.

我已经合并了一个幻灯片,它为每张幻灯片使用了Group Field元数据.

在主页上有2个标有"空项目"和"测试项目1"的帖子.如果单击空项目,您将被定向到它的单个帖子页面,在那里您将看到一个带有红色背景的".flexslider"div.如果组字段为空,那就是我要删除的div.我的意思是我完全删除div而不留空div而不是将背景颜色改为白色.

如果单击"测试项目1",将在"flexslider"幻灯片中使用可重复组字段上载图像.这是Metafields内部元数据保存的结果.

METABOX // 这是我用来注册可重复字段的代码,它允许我插入幻灯片的图像和标题.

add_action( 'cmb2_admin_init', 'gallery_metabox' );
function gallery_metabox() {
$prefix = 'gallery_';

/**
 * Repeatable Field Groups
 */
$cmb_group = new_cmb2_box( array(
    'id'           => $prefix . 'metabox',
    'title'        => __( 'Gallery', 'cmb2' ),
    'object_types' => array( 'portfolio', ),
) );

// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field( array(
    'id'          => $prefix . 'demo',
    'type'        => 'group',
    'options'     => array(
    'group_title'   => __( 'Image {#}', 'cmb2' ), // {#} gets replaced by row number
        'add_button'    => __( 'Add Another Image', 'cmb2' ),
        'remove_button' => __( 'Remove Image', 'cmb2' ),
        'sortable'      => true, // beta
        'closed'     => true, // true to have the groups closed by default
    ),
) );


$cmb_group->add_group_field( $group_field_id, array(
    'name' => __( 'Image', 'cmb2' ),
    'id'   => 'image',
    'type' => 'file',
) );

$cmb_group->add_group_field( $group_field_id, array(
    'name' => __( 'Image Caption', 'cmb2' ),
    'id'   => 'image_caption',
    'type' => 'text',
) );

 }
Run Code Online (Sandbox Code Playgroud)

我按照操作显示这些组字段的元数据.当我使用这段代码时,一切都运行得很好:

前端//

<div class="flexslider">
  <ul class="slides">

            <?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );


                foreach ( (array) $entries as $key => $entry ) {

                    $img = $img_url = $caption = '';
                if ( isset( $entry['image_id'] ) ) {
                    $img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
                        'class' => 'thumb',
                    ) );
                }
                    if ( isset( $entry['image_id'] ) ) {
                    $img_url = wp_get_attachment_image_url( $entry['image_id'], null );
                }
                $caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
                    echo '<li data-thumb="'. $img_url .'">';
                    echo $img;
                    echo $caption;
                    echo '</li>';


            } ?>
  </ul>
  </div>
Run Code Online (Sandbox Code Playgroud)

但我非常希望.flexslider仅在数据存在时显示容器+元数据.如果字段为空,那么我想显示默认文本或更好,但删除整个div本身.我尽力做研究,但我似乎无法弄清楚出了什么问题.

我也试过这段代码:

尝试//

<?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
if(empty ($entry)) { echo ''; }
else {
   foreach ( (array) $entries as $key => $entry ) {
     echo '<div class="flexslider">';
     echo '<ul class="slides">';
   $img = $img_url = $caption = '';

   if ( isset( $entry['image_id'] ) ) {
     $img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
'class' => 'thumb',
   ) );
}

if ( isset( $entry['image_id'] ) ) {
    $img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}

$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
    echo '<li data-thumb="'. $img_url .'">';
    echo $img;
    echo $caption;
    echo '</li>';
    echo '</ul>';
    echo '</div>';      
    }   
  }
 ?>
Run Code Online (Sandbox Code Playgroud)

关于上面代码的唯一好处是,当metafield为空时它肯定会删除div但是如果元数据存在则div仍然消失.

编辑//我尝试在下面的答案中使用"@stweb"代码:

$entries = get_post_meta( get_the_ID(), 'gallery_demo', true );

foreach ( (array) $entries as $key => $entry ) {
   if(empty($entry)){
      continue;
   }
   echo '<div class="flexslider">';
   echo '<ul class="slides">';
   $img = $img_url = $caption = '';

   if ( isset( $entry['image_id'] ) ) {
     $img = wp_get_attachment_image( $entry['image_id'], 'share-pick',   
   null, array(    'class' => 'thumb', ) );
   }

   if ( isset( $entry['image_id'] ) ) {
     $img_url = wp_get_attachment_image_url( $entry['image_id'], null );
   }

   $caption = isset( $entry['image_caption'] ) ? wpautop(    
   $entry['image_caption'] ) : '';
   echo '<li data-thumb="'. $img_url .'">';
   echo $img;
   echo $caption;
   echo '</li>';
   echo '</ul>';
   echo '</div>';      
} 
Run Code Online (Sandbox Code Playgroud)

但没有任何反应......红色的div只是坐在那里而不是消失.

我基本上想知道如果只将图像上传到Group Field,我才能显示第一块代码,如果没有,那么甚至根本不显示容器div.

任何人都可以解释我哪里出错了吗?

Ivn*_*hal 2

尝试这个:

$entries = get_post_meta( get_the_ID(), 'gallery_demo', true );

foreach ( (array) $entries as $key => $entry ) {
   if(empty($entry)){
      continue;
   }
   echo '<div class="flexslider">';
   echo '<ul class="slides">';
   $img = $img_url = $caption = '';

   if ( isset( $entry['image_id'] ) ) {
     $img = wp_get_attachment_image( $entry['image_id'], 'share-pick',   
   null, array(    'class' => 'thumb', ) );
   }

   if ( isset( $entry['image_id'] ) ) {
     $img_url = wp_get_attachment_image_url( $entry['image_id'], null );
   }

   $caption = isset( $entry['image_caption'] ) ? wpautop(    
   $entry['image_caption'] ) : '';
   echo '<li data-thumb="'. $img_url .'">';
   echo $img;
   echo $caption;
   echo '</li>';
   echo '</ul>';
   echo '</div>';      
} 
Run Code Online (Sandbox Code Playgroud)

更新:

foreach ( (array) $entries as $key => $entry ) {
    if ( !isset( $entry['image_id'] )  ||  $entry['image_id']  == ''  ) {
        continue;
    }
    echo '<div class="flexslider">';
    echo '<ul class="slides">';
    $img = $img_url = $caption = '';

    if ( isset( $entry['image_id'] ) ) {
       $img = wp_get_attachment_image( $entry['image_id'], 'share-pick', 
       null, array(    'class' => 'thumb', ) );
    } 

    if ( isset( $entry['image_id'] ) ) {
       $img_url = wp_get_attachment_image_url( $entry['image_id'], null );
    }

    $caption = isset( $entry['image_caption'] ) ? wpautop(         
    $entry['image_caption'] ) : '';
    echo '<li data-thumb="'. $img_url .'">';
    echo $img;
    echo $caption;
    echo '</li>';
    echo '</ul>';
    echo '</div>';      
} 
Run Code Online (Sandbox Code Playgroud)