【问题标题】:Show most recent post - if "yes" chosen as radio button value显示最近的帖子 - 如果选择“是”作为单选按钮值
【发布时间】:2017-01-09 15:34:38
【问题描述】:

这是我的单选按钮的设置:

我似乎无法让价值为 yes 的帖子显示在循环中..

这是我的循环:

<?php
$args = array( 
   'numberposts'   => 1,
   'post_type'     => 'event',
   'posts_per_page' => '1',
   'meta_key'      => 'sponsored_event',
   'meta_value'    => 'yes'
   );
   $the_query = new WP_Query( $args );
?>    
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>        
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);"></div>
<div class="sponsored-info">
     <h2>Sponsored Event</h2>
     <h1><strong><?php the_title(); ?></strong></h1>
         <p><strong>Date</strong></p><br>
         <p class="place"><?php the_field( 'event_location' ); ?></p>
         <p class="time"><?php the_field( 'event_time' ); ?></p>
         <p><?php the_field( 'excerpt' ); ?></p>
         </div>
         </div>        
<?php endwhile; else: ?>
<?php endif; ?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    这对我有用:

    <?php
    
    $args = array(
        'post_type' => 'event',
        'showposts' => 1,
        'orderby' => 'date',
        'meta_query' => array(
                array(
                'key' => 'sponsored_event',
                'value' => 1,
                'compare' => 'LIKE'
                )
            )
        );
    
        $the_query = new WP_Query( $args );
    ?>
    
        <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    
                <a href="<?php the_permalink(); ?>"><div class="sponsored-event">
                    <div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
                    </div>
                    <div class="sponsored-info">
                        <h2>Sponsored Event</h2>
                        <h1><strong><?php the_title(); ?></strong></h1>
                        <p><strong>Date</strong></p><br>
                        <p class="place"><?php the_field( 'event_location' ); ?></p>
                        <p class="time"><?php the_field( 'event_time' ); ?></p>
                        <p><?php the_field( 'excerpt' ); ?></p>
                    </div>
                </div></a>
    
    
        <?php endwhile; else: ?>
    
    <?php endif; ?>
    

    【讨论】:

      【解决方案2】:

      if ( have_posts() ) 应该引用查询:if ( $the_query-&gt;have_posts() )。

      另外,如前所述,您应该使用'posts_per_page' =&gt; 1, 而不是numberposts。

      【讨论】:

      • 这仍然没有输出任何帖子。
      【解决方案3】:

      尝试使用meta_query 来完成此操作。使用以下内容更新您的 $args:

      $args = array(
          'post_type'     => 'event',
          'posts_per_page' => '1',
          'meta_query' => array(
            array(
                'key'     => 'sponsored_event',
                'compare' => 'LIKE',
                'value'   => 'yes',
            ),
          ),
      );
      

      您还需要将if ( have_posts() ) 更新为if ( $the_query-&gt;have_posts() )。

      更多关于meta_query的信息可以在这里找到:https://codex.wordpress.org/Class_Reference/WP_Meta_Query

      【讨论】:

      • 我在 meta_query 中尝试了许多更改,但似乎没有任何效果。
      • 您的 if 语句也有问题。查看更新的答案。
      【解决方案4】:

      您可以使用任何一种,而不是同时使用 post_per_page 和 number 个帖子

      尝试以下可用的任何方法。根据ACF,建议使用Method 1,但对于WordPress,您也可以根据Method 2检索信息。

      方法:1

      $args = array(
        'posts_per_page'  => 1,
        'post_type'       => 'event',
        'meta_key'        => 'sponsored_event',
        'meta_value'  => 'yes'
      );
      $the_query = new WP_Query( $args );
      <?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
      //Your Code over here to Manipulate
      <?php endwhile; else: ?>
      <?php endif; ?>
      

      方法:2

      尝试在meta_query 中使用比较为= 运算符的元查询。

      $args = array(
          'post_type'     => 'event',
          'posts_per_page' => '1',
          'meta_query' => array(
            array(
                'key'     => 'sponsored_event',
                'compare' => '=',
                'value'   => 'yes',
            ),
          ),
      );
      

      【讨论】:

      【解决方案5】:

      我在我的机器上测试了代码,它运行良好!只是(正如其他人已经提到的)if() 语句是错误的,将其更改为:

      if ( $the_query->have_posts() )
      

      但是现在我有一个非常愚蠢的问题... 创建 ACF 字段后,您是否使用元字段(是或否)保存了一些帖子(事件)?如果没有,WP 将找不到任何东西,因为 postmeta 没有保存到数据库中。

      如果 postmeta 保存正确,您是否查看了数据库?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-16
        • 2019-10-31
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 2020-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多