在' do_not_duplicate []'之后显示下一篇文章阻止人们展示

时间:2015-12-11 18:17:47

标签: php arrays wordpress blogs

我会尽力说出最好的话:

我的博客主页有4个"最新来自:(一个类别)"由4个不同类别提供,每个类别显示2个帖子。我发现了do_not_duplicate方法并使用它来防止它们中的任何一个出现两次(因为作者在每个帖子上使用多个类别来填充我们的博客)。这很好用,但这是我的下一期:

如果一个帖子有多个类别填充主页,它只会在一个类别中发布而不会重复(按照需要),但它现在的另一个类别只显示1个帖子,我在哪里喜欢它显示2.自第二次失踪'如果重复被隐藏,我会想知道如何在该类别中显示 next (3rd)帖子。

这是我目前的代码:

 <!-- BEGIN WP PHP BLOG INSERT-->


            <?php query_posts('category_name=campuses&showposts=2'); //Get 2 most recent posts from category with slug campuses ?>
            <h2 class="cat"><?php if (have_posts()) single_cat_title("Latest from: ", true) //if there are posts in the category, display the category name in an H2 ?></h2>
            <?php if (have_posts()) while (have_posts()) : the_post(); $do_not_duplicate[] = $post->ID; // prevents the post from showing up twice on home page?> 
            <?php if ( has_post_thumbnail()) : //check to see if the post has a featured image  ?>
            <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <?php the_post_thumbnail(category-thumb); ?>
            </a>
                <?php elseif( catch_that_image() ) : ?>
                <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
            <?php endif; ?>
            <h3 class="recent"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt ()?><br class="clear" />
            <?php endwhile;?>       

            <?php query_posts('category_name=programs&showposts=2'); //Get 2 most recent posts from category with slug programs?>
            <?php if (have_posts()) single_cat_title('<h2 class="cat">Latest from: ', true) //if there are posts in the category, display the category name in an H2 ?></h2>
            <?php if (have_posts()) while (have_posts()) : the_post(); if ( in_array( $post->ID, $do_not_duplicate ) ) continue; // prevents the post from showing up twice on home page?>
            <?php if ( has_post_thumbnail()) : //check to see if the post has a featured image ?>
            <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <?php the_post_thumbnail(category-thumb); ?>
            </a>
                <?php elseif( catch_that_image() ) : ?>
                <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
            <?php endif; ?>
            <h3 class="recent"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt ()?><br class="clear" />
            <?php endwhile;?>                          

            <?php query_posts('category_name=online&showposts=2'); //Get 2 most recent posts from category with slug online?>
            <?php if (have_posts()) single_cat_title('<h2 class="cat">Latest from: ', true) //if there are posts in the category, display the category name in an H2 ?></h2>
            <?php if (have_posts()) while (have_posts()) : the_post(); if ( in_array( $post->ID, $do_not_duplicate ) ) continue; // prevents the post from showing up twice on home page?>
            <?php if ( has_post_thumbnail()) : //check to see if the post has a featured image ?>
            <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <?php the_post_thumbnail(category-thumb); ?>
            </a>
                <?php elseif( catch_that_image() ) : ?>
                <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
            <?php endif; ?>
            <h3 class="recent"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt ()?><br class="clear" />
            <?php endwhile;?>                          

            <?php query_posts('category_name=service-applied-learning&showposts=2'); //Get 2 most recent posts from category with slug service-applied-learning ?>
            <h2 class="cat"><?php if (have_posts()) single_cat_title("Latest from: ", true) //if there are posts in the category, display the category name in an H2 ?></h2>
            <?php if (have_posts()) while (have_posts()) : the_post(); if ( in_array( $post->ID, $do_not_duplicate ) ) continue; // prevents the post from showing up twice on home page?>
            <?php if ( has_post_thumbnail()) : //check to see if the post has a featured image  ?>
            <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <?php the_post_thumbnail(category-thumb); ?>
            </a>
                <?php elseif( catch_that_image() ) : ?>
                <a class="postthumb" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img height="150px" src="<?php echo catch_that_image() ?>" /></a>
            <?php endif; ?>
            <h3 class="recent"><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt ()?><br class="clear" />
            <?php endwhile;?>       

<?php if( function_exists( 'wp_pagenavi ' ) ) {
     wp_pagenavi();
} else {
     next_posts_link('Older Posts');
     previous_posts_link(' | Newer Posts');
} ?>

         </div>
</div>

    <div class="sidebar-wrapper">

        <?php get_sidebar(); ?>
    </div>


            <!--END WP PHP BLOG INSERT-->

1 个答案:

答案 0 :(得分:0)

不确定这是否会有所帮助,但看起来您需要重置query_posts();以使其再次运行

每次<?php endwhile;?>之后,您都会添加

     <?php wp_reset_query(); ?>

https://codex.wordpress.org/Function_Reference/wp_reset_query

引用: wp_reset_query()$wp_query和全局发布数据还原到原始主查询。如果必须使用该功能,则应在query_posts()之后调用此函数。如下面的示例所示,强烈建议在查询之前使用pre_get_posts过滤器来更改查询参数。

使用wp_reset_queryWP_Query后无需调用get_posts,因为这些不会修改主查询对象。而是使用wp_reset_postdata