How to hide empty query post?

Asked

Viewed 104 times

0

I created a Query post for a certain category

<div class="querypost-home" id="conteudo-home4">

<?php
$args = array(
'category_name'=>'oportunidades',
'numberposts'=>1,
'offset'=>3
);

$my_posts = get_posts($args);

if( $my_posts ):
foreach( $my_posts as $post ) : setup_postdata($post); ?>


<?php if ( has_post_thumbnail()) : ?>

   <div class="img1"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(870,420)); ?></a></div>

 <?php endif; ?>

<div class="titulo-query-home"><a href="<?php the_permalink(); ?>"><p><?php  title_lenght(40) ?></p></a></div>

  <div class="resumo-query-home"><p><?php echo get_excerpt(160); ?></p></div>
<?php 
endforeach; 
else:
?>
<p></p>
    <?php endif; ?>

</div> <!-- query post -->

I put a class in this query so I can put a nice style in these query posts. When I don’t add any posts, the BG of the query still appears, but without content. Does anyone know how to hide query when they don’t have any posts in their offset ?

2 answers

1

0

Use the following condition so that the class only appears if there is content:

 if ( $query->have_posts() ) : ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.