How to hide empty query post?

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 cool style in these query posts. Being that, when I do not add any posts, the query's BG still appears, but without content. Does anyone know how to hide the query when it has no posts in its offset?

    
asked by anonymous 05.11.2014 / 22:40

2 answers

1

Maybe the CSS selector :empty is useful in this case.

It would look like this:

.sua-classe:not(:empty){
    background-color: black;
}

A working jsFiddle to exemplify:

link

More information about :empty :

link

    
07.11.2014 / 12:19
0

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

 if ( $query->have_posts() ) : ?>
    
18.10.2018 / 20:00