自定义字段显示在其他帖子上

时间:2014-03-29 19:15:43

标签: php wordpress

我正在使用Wordpress&高级自定义字段和我有一个名为“show_additional_information”的字段,如果设置为“是”,则应显示一个额外的信息子集。但是,当填充这些字段时,它们会在所有月份记录类型上相互显示。我想我需要以某种方式将其设置为仅从显示的帖子中提取,但我不确定如何这样做。任何帮助将不胜感激

<?php query_posts(array(
 'post_type' => 'record-of-the-month', // post type with custom fields for display
 'meta_key' => 'show_additional_information', // custom field to display extra info
 'meta_compare' => '=',
 'meta_value' => 'Yes',
)
); ?>

<?php while ( have_posts() ) : the_post(); ?>

<section class='bg-cover invert-section triple-margin-bottom' style='background-image: url(<?php the_field('background_image_2'); ?>);'>
  <div class='container'>
    <div class='row'>
      <div class='col-md-2'></div>
      <div class='col-md-8 text-center'>
    <?php the_field('press_quote'); ?>
      </div>
      <div class='col-md-2'></div>
    </div>
  </div>
</section>
<section class='padding-top double-padding-bottom double-margin-bottom border-bottom'>
  <div class='container'>
    <div class='row'>
      <div class='col-md-1'></div>
       <div class='col-md-10 double-padding-right'>
    <?php the_field('artist_bio'); ?>
      </div>
      <div class='col-md-1'></div>
    </div>
  </div>
</section>
<?php endwhile;  ?>

2 个答案:

答案 0 :(得分:0)

您使用的是哪种类型的自定义字段?如果你使用的是真/假选择器,你可以使用:

$args = array(
    'post_type' => 'record-of-the-month',
    'meta_query' => array(
        array(
            'key' => 'field_name',
            'value' => '1',
            'compare' => '=='
        )
    )
);

$wp_query = new WP_Query( $args );

while ( have_posts() ) : the_post();

//

无论哪种方式,尝试使用WP_query而不是query_posts

答案 1 :(得分:0)

通过在个别字段之前放置单独的if语句来计算出来(参见下面的代码)。

<?php if( $field = get_field('background_image_2') ): ?>
<section class='bg-cover invert-section triple-margin-bottom' style='background-image: url(<?php the_field('background_image_2'); ?>);'>
  <div class='container'>
    <div class='row'>
      <div class='col-md-2'></div>
       <div class='col-md-8 text-center'>
    <?php the_field('press_quote'); ?>
      </div>
      <div class='col-md-2'></div>
    </div>
  </div>
</section>
<?php endif; ?>
<?php if( $field = get_field('artist_bio') ): ?>
<section class='padding-top double-padding-bottom double-margin-bottom border-bottom'>
  <div class='container'>
    <div class='row'>
      <div class='col-md-1'></div>
      <div class='col-md-10 double-padding-right'>
        <?php the_field('artist_bio'); ?>
      </div>
      <div class='col-md-1'></div>
    </div>
  </div>
</section>
<?php endif; ?>