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; ?>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; }