skip to Main Content

I’m trying to get the latest wp posts when I get an error that I don’t know why.

I use showpost to get the impression count but the result is always one more post.

I tried searching but don’t know where is the cause of the problem. I tried replacing showposts with posts_per_page but still the same error.

Anyone who has encountered the same error please help me
thank you!

<?php $args = array(
    'post_status' => 'publish',
    'post_type' => 'post',
    'orderby' => 'date',
    'cat' => 0,
    'order' => 'DESC',
    'showposts' => 1,
);
$getposts_related = new WP_query($args);

if ($getposts_related->have_posts()) :
    while ($getposts_related->have_posts()) : $getposts_related->the_post();
        $placeholder = get_field('logo', 'option'); ?>
        <div class="col-6 col-md-4 col-lg-4 pb-4">
            <div class="baiviet-wrap">
                <a href="<?php the_permalink(); ?>">
                    <div class="thumbnail">
                        <img src="<?php
                        $url = wp_get_attachment_url(get_post_thumbnail_id(), 'thumbnail');
                        if ($url) {
                            echo $url;
                        } else {
                            echo $placeholder;
                        }
                        ?>" alt="<?php the_title(); ?>">
                    </div>
                    <div class="tieu-de">
                        <h3><?php the_title(); ?></h3>
                    </div>
                </a>
                <div class="description">
                    <?php
                    $excerpt = the_excerpt();
                    $excerpt = substr($excerpt, 0, 200);
                    $result = substr($excerpt, 0, strrpos($excerpt, ' '));
                    echo $result; ?>
                </div>
                <div class="col-6 col-md-6 col-lg-6 cta-baiviet">
                    <a href="<?php echo get_permalink(); ?>">
                        <div class="detail-product btn-kgin kgin-sharp-5">
                            <div class="btn-wrap kgin-sharp-5">
                                <span>Xem chi tiết</span><i
                                        class="fas fa-angle-double-right"></i>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
        </div>
    <?php endwhile;
endif;
wp_reset_postdata(); ?>

2

Answers


  1. You should not use showposts

    Try using posts_per_page alongside with paged.
    Like this:

    $args = array(
        'post_status' => 'publish',
        'post_type' => 'post',
        'posts_per_page' => 1,
        'paged' => 1
        'cat' => 0,
        'orderby' => 'date',
        'order' => 'DESC',
    );
    
    Login or Signup to reply.
  2. Your code looks good
    just replace showposts with posts_per_page

    $args = array(
        'post_status' => 'publish',
        'post_type' => 'post',
        'posts_per_page' => 1,
        'orderby' => 'date',
        'cat' => 0,
        'order' => 'DESC',
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search