如何使WordPress粘贴帖子在自定义循环中工作?

时间:2013-11-26 15:13:33

标签: php html wordpress

我希望能够使用query_posts或$ custom_query = new WP_Query()创建自定义循环 并能够粘贴帖子。

例如,我在名为News的页面上有一个自定义循环,它循环“news”-category中的帖子并构建一个漂亮的Masonry网格。循环项目然后链接到实际文章。

<?php $custom_query = new WP_Query('cat=8'); // boxes loop
        while($custom_query->have_posts()) : $custom_query->the_post(); ?>
        <div <?php post_class('newsbox box '); ?> id="post-<?php the_ID(); ?>">
            <?php  //if looplink exists, looplink
                $looplink = get_post_meta( get_the_ID(), 'linkki', true );
                if( ! empty( $looplink ) ) :?> <a class="looplink" target="_blank" href="<?php $linkki = get_post_meta($id, 'linkki', true ); if( ! empty( $linkki ) ) { echo $linkki; } ?>"></a>
            <?php endif; //end looplink ?>
            <h3><?php the_title(); ?></h3>
            <?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
            <?php the_content(); ?>
        </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); // reset the query ?>

我也使用这个循环显示社交媒体小部件,所以这些应该始终是前几篇文章(=粘性)。

我并不完全满意的一个想法是不指定要循环的类别,而是指定要排除的不需要的类别。它似乎使Sticky Posts因某些原因而起作用。有没有办法使这个工作与一些简单的功能,而无需

query_posts('cat=-1,-2,-3,-4');

等所有循环?

当然,无论我是在主页中使用它们还是在显示特定类别或任何地方的自定义循环中,都会使Sticky帖子总是粘性的解决方案。

1 个答案:

答案 0 :(得分:1)

这排除了所有粘贴帖子。

query_posts( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );

要包含粘贴帖子:

    $args = array(
    'posts_per_page' => 1,
    'post__in'  => get_option( 'sticky_posts' ),
    'ignore_sticky_posts' => 1
);
query_posts( $args );

ignore_sticky_posts:忽略粘性帖子(可用于3.1版,替换了caller_get_posts参数)。默认值为0 - 不要忽略粘贴帖子。注意:忽略/排除粘贴帖子包含在返回的帖子的开头,但粘贴帖子仍将按照返回的帖子列表的自然顺序返回。