1

我对 Wordpress 主题开发非常陌生,并且发现自己在编码方面陷入了困境。

我创建了两个循环,其中一个循环只有 1 个帖子,我用更大的图像对其进行样式设置,在第二个循环中,我尝试获取帖子的其余部分,但我遇到了一些主要问题。

  1. 在第二个循环中,第一个循环的帖子也出现了,我想避免重复。

  2. 当我转到第 2 页或更远时,第一个循环的帖子也出现了,我希望它只出现在根页面上。

下面是我的代码。

第一个循环

<?php          
    $bigi = array(
        'posts_per_page'      => '1',
        'post__not_in'        => get_option( 'sticky_posts' ),
    );
    $restp = new WP_Query($bigi);
?>

<!-- Loop started for bigimage single  post -->
<?php if($restp->have_posts()) { ?>
    <?php while($restp->have_posts()) { ?>
        <?php $restp->the_post();

             $post_id = get_the_ID(); ?>

            <?php get_template_part('template/post/bigimage'); ?>

    <?php } ?>
<?php } ?> <!-- Loop ended for rest of the  post -->
<?php wp_reset_postdata(); ?>

第二循环

<?php 
    $ropl= new WP_Query(array (
        'post__not_in'  =>  get_option('sticky_posts'),
        'paged' =>  $paged,
    )); 
 ?>
<!-- Loop started for bigimage single  post -->
<?php if($ropl->have_posts()) { ?>
    <?php while($ropl->have_posts()) { ?>
        <?php $ropl->the_post(); ?>


        <?php get_template_part('template/post/rop'); ?>

    <?php } ?>
<?php } ?> <!-- Loop ended for rest of the  post -->
<?php wp_reset_postdata(); ?>
4

1 回答 1

0

这应该有效。根据您的要求更改查询。

   <?php
$args1 = array('category_name' => 'test-cat-1', 'order' => 'ASC');
$q1 = new WP_query($args);
if($q1->have_posts()) :
$firstPosts = array();
    while($q1->have_posts()) : $q1->the_post();
        $firstPosts[] = $post->ID; // add post id to array
        echo '<div class="item">';
        echo "<h2>" . get_the_title() . "</h2>";
        echo "</div>";
    endwhile;
endif;
/****************************************************************************/
// array of post id's collected in first loop, can now be used as value for the 'post__not_in' parameter in second loops query $args
$args2 = array('post__not_in' => $firstPosts, 'order' => 'ASC' );
$q2 = new WP_query($args2);
if($q2->have_posts()) :
    while($q2->have_posts()) : $q2->the_post();
        echo '<div class="item">';
        echo "<h2>" . get_the_title() . "</h2>";
        echo "</div>";
    endwhile;
endif;
?>
于 2019-09-05T10:00:16.740 回答