0

我正在这样做,使用两个 WP_Queries。在第一个中,我得到一个帖子,获取它的 ID 并将其用作下一个 WP_Query lopp 的排除参数。问题是我再次收到排除的帖子,在第二个循环中重复。我真的只需要展示特色图片和第一个项目的摘录,然后只是接下来三个项目的图块(如果您有任何建议在同一个 WP_Query 中执行所有操作,请随时告诉我)。非常感谢。

<div class="posts-by-cat">

<?php 

$args = array(
    'cat'                 => 34,
    'posts_per_page' => 1,
    'orderby' => 'rand',
);
$catquery = new WP_Query( $args );

while($catquery->have_posts()) : $catquery->the_post(); 

?>

<div class="featured-image">
<?php if ( has_post_thumbnail() ) {
    the_post_thumbnail();
   
} 
?>
</div>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?> 

<?php 

$exclude = get_the_ID(); 

$args = array(
    'cat'                 => 34,
    'posts_per_page' => 3,
    'orderby' => 'rand',
'exclude' => $exclude
);
$catquery = new WP_Query( $args );

// $catquery = new WP_Query( 'cat=34&posts_per_page=3&offset=1 ); 
?>

<ul class="postsbytag-listitem">
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<!-- p><?php the_excerpt(); ?></p -->
</li>
<?php endwhile; ?> 
<?php wp_reset_postdata(); ?>
<ul>
</div>
4

1 回答 1

1

这应该可以完成工作:

<?php

$exclude = array();

?>


<div class="posts-by-cat">

<?php 

$args = array(
    'cat'                 => 34,
    'posts_per_page' => 1,
    'orderby' => 'rand',
);
$catquery = new WP_Query( $args );

while($catquery->have_posts()) : $catquery->the_post(); 

?>

<div class="featured-image">
<?php if ( has_post_thumbnail() ) {

    // add post id to exclude array (needs to be in the first loop)
    $exclude[] = get_the_ID();

    the_post_thumbnail();
   
} 
?>
</div>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?> 

<?php 

$args = array(
    'cat'                 => 34,
    'posts_per_page' => 3,
    'orderby' => 'rand',
'post__not_in' => $exclude
);
$catquery = new WP_Query( $args );

// $catquery = new WP_Query( 'cat=34&posts_per_page=3&offset=1 ); 
?>

<ul class="postsbytag-listitem">
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<!-- p><?php the_excerpt(); ?></p -->
</li>
<?php endwhile; ?> 
<?php wp_reset_postdata(); ?>
<ul>
</div>
于 2020-09-19T23:45:22.743 回答