将div中的自定义字段值划分为两个值

时间:2015-06-06 作者:Mohamed Mokhtar

我用这个html标记对一些数据进行排序

<div class="row">
<div class="col-xs-2">
  <img src="images/member-schools/ms1.jpg" class="img-responsive">
</div>
<div class="col-xs-4">
  <h4>American International School - East</h4>
</div>
<div class="col-xs-2">
  <img src="images/member-schools/ms2.jpg" class="img-responsive">
</div>
<div class="col-xs-4">
  <h4>Dover American International School</h4>
</div>

我从我使用的可重复组元数据库中提取数据CMB2

这是注册的metabox的代码

function schools() {

// Start with an underscore to hide fields from custom fields list
$prefix = \'schools_\';

/**
 * Repeatable Field Groups
 */
$schools= new_cmb2_box( array(
    \'id\'           => $prefix . \'metabox\',
    \'title\'        => __( \'schools\', \'cmb2\' ),
    \'object_types\' => array( \'page\', ),
    \'show_on\'      => array( \'key\' => \'id\', \'value\' => 6 ),
    \'closed\'     => true,
) );

$group_field_id = $schools->add_field( array(
    \'id\'          => $prefix . \'field\',
    \'type\'        => \'group\',
    \'options\'     => array(
        \'group_title\'   => __( \'Slide {#}\', \'cmb2\' ), // {#} gets replaced by row number
        \'add_button\'    => __( \'Add Another Slide\', \'cmb2\' ),
        \'remove_button\' => __( \'Remove Slide\', \'cmb2\' ),
        \'sortable\'      => true, // beta
    ),
) );


$schools->add_group_field( $group_field_id, array(
    \'name\' => __( \'Photo\', \'cmb2\' ),
    \'id\'   => \'photo\',
    \'type\' => \'file\',
) );

$schools->add_group_field( $group_field_id, array(
    \'name\' => __( \'Name\', \'cmb2\' ),
    \'id\'   => \'name\',
    \'type\' => \'text\',
) );}
我通过这个代码提取数据

$schools = get_post_meta( get_the_ID(), \'schools_field\', true );
其中$schools是一个数组,其中包含每个条目的图像和标题,我可以通过循环提取值。我的问题是如何添加<div class="row"> 该数组中的每两个项,因为我需要为每两个条目对应一行

编辑:var\\u转储结果

array(3) {
    [0] => array(3) {
        ["photo_id"]    => string(2) "89"
        ["photo"]       => string(56) "http://localhost/NCSR/wp-content/uploads/2015/06/ms3.jpg"
        ["name"]        => string(8) "School 1" 
    }
    [1] => array(3) {
        ["photo_id"]    => string(2) "88"
        ["photo"]       => string(56) "http://localhost/NCSR/wp-content/uploads/2015/06/ms2.jpg"
        ["name"]        => string(8) "school 2"
    }
    [2] => array(3) {
        ["photo_id"]    => string(2) "87"
        ["photo"]       => string(56) "http://localhost/NCSR/wp-content/uploads/2015/06/ms1.jpg"
        ["name"]        => string(8) "school 3"
    } 
}

2 个回复
SO网友:Mohamed Mokhtar

我找到了一种方法,结果很简单,这就是我所做的

$Aschools = get_post_meta( get_the_ID(), \'american_member_schools_field\', true );

$i = 0;

$entries = count($Aschools);

foreach ( (array) $Aschools as $key => $Aschool ) {

  $logo = $Aname = \'\';

  $i++;


  if ( isset( $Aschool[\'name\'] ) )
      $Aname =  $Aschool[\'name\'] ;

  if ( isset( $Aschool[\'logo_id\'] ) ) {
      $logo = wp_get_attachment_image( $Aschool[\'logo_id\'], \'full\', null, array(
          \'class\' => \'img-responsive\',
      ) );
  }

  // Do something with the data

  if ($i & 1) {
    echo \'<div class="row row-eq-height">\';
    echo \'<div class="col-xs-2">\';
    echo $logo;
    echo \'</div>\';
    echo \'<div class="col-xs-4">\';
    echo \'<h4>\' . $Aname . \'</h4>\';
    echo \'</div>\';
  } elseif ($i & 1 && $i == $entries) {
    echo \'<div class="row row-eq-height">\';
    echo \'<div class="col-xs-2">\';
    echo $logo;
    echo \'</div>\';
    echo \'<div class="col-xs-4">\';
    echo \'<h4>\' . $Aname . \'</h4>\';
    echo \'</div>\';
    echo \'</div>\';
  } else {
    echo \'<div class="col-xs-2">\';
    echo $logo;
    echo \'</div>\';
    echo \'<div class="col-xs-4">\';
    echo \'<h4>\' . $Aname . \'</h4>\';
    echo \'</div>\';
    echo \'</div>\';
  }
}

SO网友:Omar Tariq
display_schools($string);

function display_schools($string) {
    $counter = 0;
    $i = 0;
    $len = count($string);
    foreach($string as $school) {
        if($counter == 0) {
            echo \'<div class="row">\';
        }
        ?>
            <div class="col-xs-2">
              <img src="<?php echo $school[\'photo\']; ?>" class="img-responsive photo_id_<?php echo $school[\'photo_id\']; ?>">
            </div>
            <div class="col-xs-4">
              <h4><?php echo $school[\'name\']; ?></h4>
            </div>
        <?php
        if($counter == 1 || $i == $len - 1) {
            echo \'</div>\';
            $counter = 0;
        } else {
            $counter = $counter + 1;    
        }

        $i++;
    }
}
结束

相关推荐

php if username is role

我知道,如果某个登录用户是某个角色,您可以使用<?php if ( current_user_can(\'contributor\') ) : ?> media files loop <?php endif; ?> 然而,我想做同样的事情,但对于一个特定的用户名,而不是登录的用户。我在wordpress网站上有用户配置文件,其中一些是艺术家,如果查看的配置文件是艺术家,我想显示他们上传的媒体。(如果正在查看的配置文件是另一个角色,则不会返回任何内容