我正在使用自定义帖子类型的投资组合(ID为3)运行两个循环。第一个循环用于精选,第二个循环用于其余部分。我计划以随机顺序发布超过3个精选帖子。我希望在第二个循环中显示未在第一个循环中显示的特色。我如何设置它以便没有重复的帖子?
<?php
/*
Template Name: Portfolio
*/
get_header(); ?>
<div class="section-bg">
<div class="portfolio">
<div class="featured-title">
<h1>featured</h1>
</div> <!-- end #featured-title -->
<div class="featured-gallery">
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => 3, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
</a>
</div> <!-- end .featured -->
<?php endwhile; ?>
</div> <!-- end .featured-gallery -->
<div class="clearfix"></div>
</div> <!-- end .portfolio -->
</div> <!-- end #section-bg -->
<div class="clearfix"></div>
<div class="section-bg">
<div class="portfolio-gallery">
<?php
$args = array( 'post_type' => 'portfolio', 'orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?></a>
</a>
</div> <!-- end .featured -->
<?php endwhile; ?>
<div class="clearfix"></div>
</div> <!-- end .portfolio-gallery -->
<div class="clearfix"></div>
</div> <!-- end #section-bg -->
<?php get_footer(); ?>
如果可能,答案是否可以概述如何将其实现到现有代码中?谢谢。 :)
答案 0 :(得分:0)
在第二个循环中,排除ID为3的猫。使用负值
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => -3, 'orderby' => 'rand' );
请参阅codex以获取参考资料
答案 1 :(得分:0)
我在http://webmasters-blog.com/how-to-avoid-duplicate-posts-in-multiple-loops/
上找到了解决方案<?php
/*
Template Name: Portfolio
*/
get_header(); ?>
<div class="section-bg">
<div class="portfolio">
<div class="featured-title">
<h1>featured</h1>
</div> <!-- end #featured-title -->
<div class="featured-gallery">
<?php
$ids = array();
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => 3, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
</a>
</div> <!-- end .featured -->
<?php $ids[]= $post->ID; endwhile; ?>
</div> <!-- end .featured-gallery -->
<div class="clearfix"></div>
</div> <!-- end .portfolio -->
</div> <!-- end #section-bg -->
<div class="clearfix"></div>
<div class="section-bg">
<div class="portfolio-gallery">
<?php
$args = array( 'post_type' => 'portfolio', 'orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if (!in_array($post->ID, $ids)) {
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?></a>
</a>
</div> <!-- end .featured -->
<?php } endwhile; ?>
<div class="clearfix"></div>
</div> <!-- end .portfolio-gallery -->
<div class="clearfix"></div>
</div> <!-- end #section-bg -->
<?php get_footer(); ?>
如果有更清洁的方法去做,我仍然会感激反馈!
答案 2 :(得分:0)
当然,有一件事你可以改进:
$args = array( 'post_type' => 'portfolio', 'orderby' => 'rand', 'post__not_in' => $ids);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
并且......在你的functions.php中为the_title()
添加一个过滤器也可以,做一下substr事情,这样你就可以简单地使用the_title()
。
答案 3 :(得分:0)
可能是此链接有助于防止重复的帖子 here