I'm trying to do a query in my index.php
page to get some posts from my blogue
category.
In my other pages this query works perfectly but on the index.php
returns the most recent post 3 times.
They query:
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'cat' => '',
'category_name' => 'blogue',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
$wqBlog = get_posts($args);
the html:
<section class="container">
<div class="cards display--flex ">
<?php foreach ($wqBlog as $blogPost): setup_postdata( $blogPost ); ?>
<div class="card">
<?php if(has_post_thumbnail( )) : ?>
<div class="card__image display--flex">
<!--imagem -->
<?php the_post_thumbnail( ); ?>
</div> <!-- imagem -->
<?php else : ?>
<div class="card__image__none">
</div>
<?php endif;?>
<div class="card__container">
<p class="card__container__subtitle">
<?php echo get_the_date(); ?>
</p>
<h2 class="card__container__title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
I'm trying to do a query in my index.php
page to get some posts from my blogue
category.
In my other pages this query works perfectly but on the index.php
returns the most recent post 3 times.
They query:
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'cat' => '',
'category_name' => 'blogue',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => '',
);
$wqBlog = get_posts($args);
the html:
<section class="container">
<div class="cards display--flex ">
<?php foreach ($wqBlog as $blogPost): setup_postdata( $blogPost ); ?>
<div class="card">
<?php if(has_post_thumbnail( )) : ?>
<div class="card__image display--flex">
<!--imagem -->
<?php the_post_thumbnail( ); ?>
</div> <!-- imagem -->
<?php else : ?>
<div class="card__image__none">
</div>
<?php endif;?>
<div class="card__container">
<p class="card__container__subtitle">
<?php echo get_the_date(); ?>
</p>
<h2 class="card__container__title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
Share
Improve this question
asked Oct 10, 2019 at 16:23
Pedro FerreiraPedro Ferreira
1791 silver badge10 bronze badges
1 Answer
Reset to default 1Instead of get_posts, why don't you use WP_Query instead?
I removed a few arguments because they are the default and can be omitted.
$args = array(
'posts_per_page' => 3,
'category_name' => 'blogue',
'suppress_filters' => true,
);
$wqBlog = new WP_Query($args);
And in your HTML
<?php if( $wpBlog->have_posts() ): ?>
<section class="container">
<div class="cards display--flex ">
<?php while ( $wqBlog->have_posts() ): $wpBlog->the_post(); ?>
<div class="card">
<?php if(has_post_thumbnail( )) : ?>
<div class="card__image display--flex">
<!--imagem -->
<?php the_post_thumbnail( ); ?>
</div> <!-- imagem -->
<?php else : ?>
<div class="card__image__none">
</div>
<?php endif;?>
<div class="card__container">
<p class="card__container__subtitle">
<?php echo get_the_date(); ?>
</p>
<h2 class="card__container__title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</section>
<?php endif; ?>