微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每页帖子不适用于 wordpress 中的粘性帖子查询

如何解决每页帖子不适用于 wordpress 中的粘性帖子查询

this is my post loop that i am using


                  <?PHP
                        $sticky = get_option( 'sticky_posts' );
                            rsort( $sticky );
                                $args = array(
                                'post_type'           => 'post','post__in'            => $sticky,'posts_per_page'      => 1
                                );
                           $sticky_query = new WP_Query( $args );
                          while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
                      ?>
                    <article class="cust-arc-post">
                      <img src="<?PHP the_post_thumbnail_url(); ?>" alt="">
                      <div class="arc-post-header">
                        <a href="<?PHP the_permalink(); ?>"><h3><?PHP the_title(); ?></h3></a>
                        <a class="cat" href="javascript:;">Category Title</a>
                      </div>
                      <p><?PHP echo wp_trim_words( get_the_content(),20,'...' ); ?></p>
                    </article>
                      <?PHP endwhile;
                        wp_reset_postdata();
                      ?>

我尝试使用 offset 但没有成功,

我认为我的循环有问题

如果有人能帮我解决这个问题 提前致谢

解决方法

您需要的所有信息都可以在theme handbook


如代码中所述,这仅显示第一个置顶帖子,如果没有则返回最后发布的帖子:

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

您的问题可能出在 rsort(); 函数中,因为它正在从最高到最低反转数组。

,

试试下面的代码。

<?php
    $sticky = get_option( 'sticky_posts' );
    rsort( $sticky );
    $posts_per_page = 12;
    $sticky_count = count($sticky);
    
    if ($sticky_count < $posts_per_page) {
        $posts_per_page = $posts_per_page - $sticky_count;
    } else {
        $posts_per_page = 1;
    }

    $args = array(
        'post_type'      => 'post','post__in'       => $sticky,'posts_per_page' => $posts_per_page
    );
    $sticky_query = new WP_Query( $args );
    while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
        <article class="cust-arc-post">
            <img src="<?php the_post_thumbnail_url(); ?>" alt="">
            <div class="arc-post-header">
                <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
                <a class="cat" href="javascript:;">Category Title</a>
            </div>
            <p><?php echo wp_trim_words( get_the_content(),20,'...' ); ?></p>
        </article>
    <?php endwhile; wp_reset_postdata(); ?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。