显示最近的 post - 如果选择 "yes" 作为单选按钮值
Show most recent post - if "yes" chosen as radio button value
这是我的单选按钮的设置:
我似乎无法让值为 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; ?>
尝试使用 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->have_posts() )
。
有关 meta_query
的更多信息可在此处找到:https://codex.wordpress.org/Class_Reference/WP_Meta_Query
除了使用 post_per_page 和数字帖子之外,您还可以使用您选择的任何一个帖子
尝试以下可用的任何方法。根据 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
.
中使用 compare as =
运算符尝试元查询
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => '=',
'value' => 'yes',
),
),
);
if ( have_posts() )
应引用查询:if ( $the_query->have_posts() )
.
此外,如前所述,您应该使用 'posts_per_page' => 1,
而不是 numberposts
。
这对我有用:
<?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; ?>
我在我的机器上测试了代码,它工作正常!只是(正如其他人已经提到的) if()
声明是错误的,将其更改为:
if ( $the_query->have_posts() )
但现在我有一个非常愚蠢的问题...创建 ACF 字段后,您是否使用元字段(是或否)保存了一些帖子(事件)?如果没有,WP 将找不到任何东西,因为 postmeta 没有保存到数据库中。
如果 postmeta 保存正确,您是否检查过数据库?
这是我的单选按钮的设置:
我似乎无法让值为 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; ?>
尝试使用 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->have_posts() )
。
有关 meta_query
的更多信息可在此处找到:https://codex.wordpress.org/Class_Reference/WP_Meta_Query
除了使用 post_per_page 和数字帖子之外,您还可以使用您选择的任何一个帖子
尝试以下可用的任何方法。根据 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',
),
),
);
if ( have_posts() )
应引用查询:if ( $the_query->have_posts() )
.
此外,如前所述,您应该使用 'posts_per_page' => 1,
而不是 numberposts
。
这对我有用:
<?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; ?>
我在我的机器上测试了代码,它工作正常!只是(正如其他人已经提到的) if()
声明是错误的,将其更改为:
if ( $the_query->have_posts() )
但现在我有一个非常愚蠢的问题...创建 ACF 字段后,您是否使用元字段(是或否)保存了一些帖子(事件)?如果没有,WP 将找不到任何东西,因为 postmeta 没有保存到数据库中。
如果 postmeta 保存正确,您是否检查过数据库?