将每页的帖子分成两部分

Break up posts per page in two sections

我有一个名为 'videos' 的自定义 post 类型,我使用带有 select 字段的高级自定义字段插件作为过滤器来分配 [=16] =] 部分将显示在我的页面上。我有两列,一列称为“我们的工作”,另一列称为 'featured films'。我需要每个部分显示最新的 4 post,但是当我更改 posts_per_page 时,它会影响总数,有没有办法将每个查询限制为 4 个?子问题可以两次使用相同的查询 运行 吗?我的代码是:

<div class="triple ourWork col-sm-6">
    <h2>Our Work</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page' 
=> 4) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>

<?php if( get_field('labeled_as') == 'our work' ): ?>
        <div class="col-sm-6" id="">
            <?php global $post; 
                $gethref = $post->post_name;
            ?>
        <div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><a href="/<?php echo $gethref ?>"><span class="play"><?php the_title(); ?></span></a></div>
            <p><?php echo the_field('issue_short_description'); ?></p>
        </div>
    <?php endif; ?>

<?php endwhile; ?>
</div>

<div class="triple featuredFilms col-sm-6">
    <h2>Featured Films</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page' => 4 ) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>

    <?php if( get_field('labeled_as') == 'featured film' ): ?>
        <div class="col-sm-6" id="">
            <?php global $post; 
                $gethref = $post->post_name;
            ?>
        <div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><a href="/<?php echo $gethref ?>"><span class="play"><?php the_title(); ?></span></a></div>
            <p><?php echo the_field('issue_short_description'); ?></p>
        </div>
    <?php endif; ?>

<?php endwhile; ?>
</div>

您想对 acf 字段使用元查询。 https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

我个人会在 2 个循环中使用 2 个变量进行调试等。

$work_loop = new WP_Query( 
  array( 
    'post_type' => 'videos', 
    'posts_per_page' => 4,
    'meta_key'      => 'labeled_as',
    'meta_value'    => 'our work'
   ) 
);

这将仅获取具有该标签的帖子。按照您目前的操作方式,您会得到 4 个结果,然后过滤标签。因此,如果 3 个在另一个标签中,则只会显示 1 个结果。这让你 4 将显示。只需更改第二个循环的元值,您就可以开始了。