【问题标题】:Get only some of the values from array仅从数组中获取一些值
【发布时间】:2017-03-24 14:41:58
【问题描述】:

我使用代码从数据库中检索值。问题是它显示了所有值。我只需要显示两个值及其名称。名称为 External 和 Visual 的值

<?php $attributes = Custom_Post_Types::get_attributes(); ?>
<?php if ( ! empty( $attributes ) && is_array( $attributes ) && count(         $attributes ) > 0 ) : ?>

<?php foreach( $attributes as $key => $attribute ) : ?>  

                <dt><?php echo wp_kses( $attribute['name'], wp_kses_allowed_html( 'post' ) ); ?></dt>
                    <dd><?php echo wp_kses( $attribute['value'], wp_kses_allowed_html( 'post' ) ); ?></dd> 
<?php endforeach; ?>                 
<?php endif; ?>

【问题讨论】:

  • array_filter()。或者只在数据库中查询您实际需要的数据,而不是全部获取并在 PHP 中过滤

标签: php arrays wordpress attributes


【解决方案1】:

然后添加对这两个值的检查。请参阅下面的示例代码:

<?php foreach( $attributes as $key => $attribute ) : ?>  
        <?php if ($attribute['name'] == 'External'  || $attribute['name'] == 'Visual'){
            <dt><?php echo wp_kses( $attribute['name'], wp_kses_allowed_html( 'post' ) ); ?></dt>
            <dd><?php echo wp_kses( $attribute['value'], wp_kses_allowed_html( 'post' ) ); ?></dd> 
         <?php } ?> 

【讨论】:

  • 不工作,如果。我想我在变量中犯了一个错误。我联系了模板的作者,并建议我使用函数 Inventor_Post_Types::get_metabox_field_value() 。我还是试试。这些是函数
  • public static function get_metabox_field_value( $metabox_id, $field_id, $post_id = null, $context = 'overview' ) { $field = cmb2_get_field( $metabox_id, $field_id ); return self::get_field_value( $field-&gt;args, $post_id, $context ); } /** public static function get_field_value( $field, $post_id = null, $context = 'overview' ) { if ( empty( $post_id ) ) { $post_id = get_the_ID(); } $value = get_post_meta( $post_id, $field['id'], true ); if ( empty( $value ) &amp;&amp; 0 !== strpos( $field['type'], 'taxonomy_' ) ) { return null; }
猜你喜欢
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 2014-09-12
相关资源
最近更新 更多