Search code examples
wordpressshortcode

Wordpress event shortcode not displaying message when no posts available


I have a shortcode created, which display any posts in a custom post type called "event".

Everything works well, but I want it to display a message if no posts are available. I've tried a few things trying to incorporate , but it just causes an error.

I tried this (see below) but it doesn't display the message "* no events listed at this time..." just a blank page.

Any suggestions on what the correct code is to use?

    add_shortcode('events', 'Events');
    function events($atts){

$atts = shortcode_atts( array(
    'category' => ''
), $atts );

    $categories  = explode(',' , $atts['category']);

$args = array(
        'post_type'     => 'event',
        'post_status'   => 'publish',
        'meta_key'      => 'event_date',
        'orderby'       => 'meta_value_num',
        'order'         => 'ASC',
        'posts_per_page'=> -1,
        'tax_query'     => array( array(
                            'taxonomy'  => 'category',
                            'field'    => 'slug',
                            'operator' => 'AND',
                            'terms'     => $categories
                        ) )
    );

ob_start();


$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
    ?>


        <?php
        while ($my_query->have_posts()) : $my_query->the_post(); ?>

        <div><?php the_title(); ?></div>
        <div><?php the_content(); ?></div>

        
    <?php endwhile; ?>
    <p>* no events listed at this time...</p>
    <?php wp_reset_query(); ?>

    <?php 
}

$retVal = ob_get_contents();
ob_end_clean();

return $retVal;
    }

Solution

  • I have checked your code is not proper.

    Please use the below code.

    <?php
    
    add_shortcode( 'events', 'callback_function_name' );
    
    function callback_function_name( $atts, $shrt_content = null ) {
    
        // Shortcode Parameter
        $atts = shortcode_atts(array(
            'category' => ''
        ), $atts, 'events');
    
        $categories  = ! empty( $atts['category'] ) ? explode( ',' , $atts['category'] ) : '';
    
        extract( $atts );
    
        // Query args
        $query_args = array(
                'post_type'         => 'event',
                'post_status'       => 'publish',
                'meta_key'          => 'event_date',
                'orderby'           => 'meta_value_num',
                'order'             => 'ASC',
                'posts_per_page'    => -1,
                'tax_query'         => array( array(
                                        'taxonomy'  => 'category',
                                        'field'     => 'slug',
                                        'operator'  => 'AND',
                                        'terms'     => $categories
                                    ) )
            );
    
        // WP Query for Events
        $post_query = new WP_Query( $query_args );
    
        ob_start();
    
        // If post is there
        if ( $post_query->have_posts() ) {
    
            while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
    
                <div><?php the_title(); ?></div>
                <div><?php the_content(); ?></div>
    
            <?php endwhile;
    
        } else { ?>
    
            <p>* no events listed at this time...</p>
    
        <?php }
    
        wp_reset_postdata(); // Reset WP Query
    
        $content .= ob_get_clean();
        return $content;
    }