Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 避免在Wordpress的多个循环中重复自定义帖子类型的帖子_Php_Wordpress - Fatal编程技术网

Php 避免在Wordpress的多个循环中重复自定义帖子类型的帖子

Php 避免在Wordpress的多个循环中重复自定义帖子类型的帖子,php,wordpress,Php,Wordpress,我用一个自定义的post类型的公文包(ID为3)运行两个循环。第一个循环用于特色,第二个循环用于其他循环。我计划以随机顺序发布3篇以上的特色文章。我想让那些没有在第一个循环中显示的特色产品出现在我的第二个循环中。我如何设置此设置以避免出现重复帖子 <?php /* Template Name: Portfolio */ get_header(); ?> <div class="section-bg"> <div class="portfo

我用一个自定义的post类型的公文包(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(); ?>

作为特色的

如果可能,答案能否概述如何在我现有的代码中实现它?谢谢。:)

在第二个循环中,排除ID为3的猫。使用负值

 $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => -3, 'orderby' => 'rand' );
请参阅参考

我在上找到了解决方案


作为特色的

如果有一个更干净的方法去做这件事,我仍然会感谢反馈

当然,有一件事你可以改进:

  $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中为
标题()添加一个过滤器也很好,可以执行substr操作,因此您可以简单地使用
标题()

我计划以随机顺序发布3篇以上的特色文章。我想让那些没有在第一个循环中显示的特色产品出现在我的第二个循环中。这可能吗?是的,在第一个循环中,您必须将要可视化的帖子保存在数组中,在第二个循环中,您必须添加一个参数以跳过此帖子,帖子不在!对不起,我没有太多的编码背景。。。你认为你能告诉我如何在我现有的代码中实现它吗?我为这件麻烦事道歉!不,这或多或少是我告诉你的!唯一的区别是,您两次查询特色帖子,并在代码中跳过。如果你的网站没有太多的流量,这就是irrilevant。这个链接对防止重复发布有用吗
  $args = array( 'post_type' => 'portfolio', 'orderby' => 'rand', 'post__not_in' => $ids);
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();