简体   繁体   English

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

[英]How to make WordPress sticky posts work in custom loops?

I want to be able to create custom loops using query_posts or $custom_query = new WP_Query() and be able to make sticky posts. 我希望能够使用query_posts或$ custom_query = new WP_Query()创建自定义循环,并能够发布即时贴。

For example I have a custom loop on a page called News, which loops the posts in 'news' -category and builds a nice Masonry grid out of them. 例如,我在一个名为“新闻”的页面上有一个自定义循环,该循环使“新闻”类别中的帖子循环,并根据它们构建一个漂亮的Masonry网格。 The looped items then link to the actual article. 循环的项目然后链接到实际的文章。

<?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 ?>

I'm also using this loop to display ie social media widgets, so these should always be the first couple of articles (=sticky). 我还使用此循环显示社交媒体窗口小部件,因此这些应该始终是前几篇文章(=粘性)。

One idea which I'm not totally satisfied with is to not specif a category to loop, but specifying the unwanted categories to exclude. 我不完全满意的一个想法是不指定要循环的类别,而是指定要排除的不需要的类别。 It seems to make Sticky Posts work for some reason. 似乎由于某些原因,Sticky Posts可以工作。 Is there a way to make this work with some simple function without having to 有没有一种方法可以使此函数具有一些简单的功能,而不必

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

etc for all the loops? 等所有循环?

Of course a solution that would make the Sticky posts always sticky, no matter if I'm using them in the home page or custom loop showing a specfic category or wherever. 当然,无论我是在主页上还是在显示特定类别的自定义循环中或任何地方使用它,都可以使粘性帖子始终保持粘性的解决方案。

This excludes all the sticky posts. 这不包括所有粘性帖子。

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

To include sticky posts: 包括粘性帖子:

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

ignore_sticky_posts : ignore sticky posts or not (available with Version 3.1, replaced caller_get_posts parameter). ignore_sticky_posts:是否忽略粘性帖子(在3.1版中可用,已替换caller_get_posts参数)。 Default value is 0 - don't ignore sticky posts. 默认值为0-不要忽略粘性帖子。 Note: ignore/exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned. 注意:忽略/排除粘性帖子被包括在返回的帖子的开头,但是粘性帖子仍将按照返回的帖子列表的自然顺序返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM