Php 如何在Wordpress中显示带有WP_查询的特色文章
我有两行,最上面的一行包含特色文章,有两列,最下面的一行包含最新文章,每行有三列 我使用WP_查询在最下面一行抓取并显示最新的文章,效果很好。但是,我如何选择特定的文章作为特色,并在顶行通过单个查询显示它们,即使每列的宽度不同。第一列是col-md-4,第二列是col-md-8 我已经安装了高级自定义字段,并创建了一个新字段复选框以附加到文章中,当我创建或编辑一篇新文章时,我可以选中该复选框,告诉它这是一篇特色文章。然后我可以使用$variable=get_field'featured'获取数据,但我不知道如何使用高级自定义字段显示循环中的特色文章 如果有更好的方法,请让我知道,因为我没有主意了 这是到目前为止我的代码Php 如何在Wordpress中显示带有WP_查询的特色文章,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我有两行,最上面的一行包含特色文章,有两列,最下面的一行包含最新文章,每行有三列 我使用WP_查询在最下面一行抓取并显示最新的文章,效果很好。但是,我如何选择特定的文章作为特色,并在顶行通过单个查询显示它们,即使每列的宽度不同。第一列是col-md-4,第二列是col-md-8 我已经安装了高级自定义字段,并创建了一个新字段复选框以附加到文章中,当我创建或编辑一篇新文章时,我可以选中该复选框,告诉它这是一篇特色文章。然后我可以使用$variable=get_field'featured'获取数据
<div class="row" id="featured-top">
<div class="col-md-4">
<figure class="snip1253">
<div class="image" style="background: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/331810/sample52.jpg') center center / cover"></div>
<figcaption>
<h3>The World Ended Yesterday</h3>
<p>
I don't need to compromise my principles, they don't have the slightest bearing.
</p>
</figcaption>
<footer>
<div class="comments">
<span class="fa-stack fa-2x">
<i class="fa fa-comment fa-stack-1x"></i>
<strong class="fa-stack-1x fa-stack-text fa-inverse">5</strong>
</span>
</div>
</footer>
<a href="#"></a>
</figure>
</div>
<div class="col-md-8" id="big-col">
<figure class="snip1253" style="background: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/331810/sample52.jpg') center center / cover">
<div class="image"></div>
<figcaption class="overlay">
<h3>The World Ended Yesterday</h3>
<p>
I don't need to compromise my principles, they don't have the slightest bearing on what happens to me anyway.
</p>
</figcaption>
<footer class="no-border">
<div class="comments">
<span class="fa-stack fa-2x">
<i class="fa fa-comment fa-stack-1x"></i>
<strong class="fa-stack-1x fa-stack-text fa-inverse">5</strong>
</span>
</div>
</footer>
</figure>
</div>
</div>
<div class="row" id="featured-list">
<?php
wp_reset_query();
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => 6,
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<div class="col-md-4">
<figure class="snip1253">
<div class="image" style="background: url('<?php the_post_thumbnail_url(); ?>') center center / cover"></div>
<figcaption>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<p class="the-date"><?php the_author_posts_link(); ?> - <?php echo get_the_date(); ?></p>
</figcaption>
<footer>
<div class="comments">
<span class="fa-stack fa-2x">
<i class="fa fa-comment fa-stack-1x"></i>
<strong class="fa-stack-1x fa-stack-text fa-inverse"><a href="<?php comments_link(); ?>"><?php comments_number('0','1','%' );?></a></strong>
</span>
</div>
</footer>
</figure>
</div>
<?php
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
</div>
特色顶部是我想展示特色文章的地方
如果我可以在第二个循环中包含第一列col-md-4,那对我来说就更好了,所以我只需要在第二列col-md-8中显示特色文章,但我不确定这是否可行。您需要通过WP\u查询按acf自定义字段进行查询,如下所示:
$args = [
'posts_per_page' => 2,
'meta_query' => [
[
'key' => 'featured',
'value' => 1
]
]
];
$q = new WP_Query($args);
您需要完成的唯一部分是检查您是否正在考虑第一篇或第二篇文章,并更改生成的容器
编辑
要使用这些结果,请复制代码:
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$class = $q->current_post == 1 ? 'col-md-4' : 'col-sm-8';
....
我不想使用acf,但是_特性是从哪里来的?这个查询没有检索到任何东西。它可以工作。在meta_查询中只有2个数组,但它只需要一个。是的,问题是如何使此查询与col-md-4和col-md一起工作-8@Halnex请参见我的编辑,您只需与模板集成即可。