Home>Support>Add Else Statement to this Function

Add Else Statement to this Function

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’);

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 1 year, 2 months ago Andrew Misplon Hi, I Work Here

    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

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More