Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Wordpress cómo verificar si una consulta tiene publicaciones

Este código me permite mostrar al usuario conectado la lista de sus artículos escritos. Sin embargo, me gustaría elegir el mensaje que aparece cuando no se han escrito artículos (cuando la lista está vacía).

Sé que necesito una declaración IF pero realmente no sé dónde ponerla y con qué datos.

Me gustaría que el usuario que no tiene artículos vea escrito "sin artículos".

Gracias por adelantado,

Aquí está mi código:

 add_action( 'woocommerce_account_dashboard' , 'recent_posts', 3 ); function recent_posts() { if ( is_user_logged_in() ): global $current_user; wp_get_current_user(); $author_query = array('posts_per_page' => '-1','author' => $current_user->ID); $author_posts = new WP_Query($author_query); ?><div id="recentposts"> <ul class="liststylenone"> <?php while($author_posts->have_posts()) : $author_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?></ul><?php else : echo "not logged in"; endif; ?>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Cuando usa wp_query , tiene una propiedad llamada found_posts . Justo antes de su etiqueta ul , vamos a verificar si hay alguna publicación en su consulta. Si no se encuentra ninguna publicación, vamos a hacer eco de una etiqueta p con un mensaje. Al igual que:

 add_action('woocommerce_account_dashboard', 'recent_posts', 3); function recent_posts() { if (is_user_logged_in()) : global $current_user; $author_query = array('posts_per_page' => '-1', 'author' => $current_user->ID); $author_posts = new WP_Query($author_query); ?> <div id="recentposts"> <?php if ($author_posts->found_posts) { ?> <ul class="liststylenone"> <?php while ($author_posts->have_posts()) : $author_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> <?php } else { echo '<p class="author-no-post-yet">No Articles</p>'; } else : echo "not logged in"; endif; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!