How can I add an else statement to this shortcode function so that if there are no jobs postings, the output would be “There are no listings at this time.”
/* Jobs Widget Shortcode */
function vantage_jobs_posts_shortcode($atts, $content = null) {
global $post;
extract(shortcode_atts(array(
‘category_name’ => ‘jobs’,
‘num’ => ‘5’,
‘order’ => ‘DESC’,
‘orderby’ => ‘post_date’,
), $atts));
$args = array(
‘category_name’ => $category_name,
‘posts_per_page’ => $num,
‘order’ => $order,
‘orderby’ => $orderby,
);
$output = ”;
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
$output .= ‘‘.get_the_title().’
‘.get_the_date().’
‘;
}
wp_reset_postdata();
return ”. $output .”;
}
add_shortcode(‘vantage_jobs_posts’, ‘vantage_jobs_posts_shortcode’);
Hi, thanks for reaching out.
Unfortunately, your question is a bit beyond the free support we’re able to offer here on the forums. We’re able to dive a bit deeper within our premium support scope and assist with questions of this nature.
You could perhaps head over to WordPress Stack Exchange. The following post looks at how to check the return value of
get_posts
and write a conditional to deal with the result.https://wordpress.stackexchange.com/questions/218169/check-return-value-of-get-posts