skip to Main Content
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();
                      ?>

i tried using offset but no luck,

i think something is wrong with my loop

if anyone can help me with this
thanks in advance

2

Answers


  1. All the information you need can be found in the theme handbook


    As stated in the codex, this displays just the first sticky post, if none return the last post published:

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

    Your problem likely lies in the rsort(); function, because it’s reversing the array from highest to lowest.

    Login or Signup to reply.
  2. Try the below code.

    <?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(); ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search