admin管理员组

文章数量:1391969

I wrote a shortcode to display all my jobs. When there are no jobs, I want to display a message instead of the available jobs. I used an else-statement to do this, but when the message shows, it shows on top of the page instead of the place where I added the shortcode.

My code:

function dfib_jobs_shortcode( $atts ) {
    ob_start();

    $query = new WP_Query( array(
        'post_type' => 'jobpost',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'orderby' => 'rand',
    ) ); ?>
    <div class="vacatures__wrapper">
        <?php if ( $query->have_posts() ) { ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="vacature__item">
                    <h4 class="vacature__title"><?php the_title() ?></h4>
                    <?php if ( has_excerpt() ) { 
                        the_excerpt();
                    } else {
                        the_content();
                    } ?>
                    <a class="vacature__btn" href="<?php the_permalink() ?>"><?php _e( 'meer info', 'vdp' ) ?></a>
                </div>
            <?php endwhile; ?>
            <div class="vacature__item spontaan">
                <h4 class="vacature__title"><?php _e( 'Spontaan solliciteren?', 'vdp' ) ?></h4>
                <a class="vacature__btn" href="<?php _e( '/spontaan-solliciteren', 'vdp' ) ?>"><?php _e( 'solliciteren', 'vdp' ) ?></a>
            </div>
            <?php wp_reset_postdata();
            $myvariable = ob_get_clean();
            return $myvariable;
        } else { ?>
            <div class="vacature__item spontaan">
                <h4 class="vacature__title"><?php _e( 'Momenteel zijn er geen vacatures!', 'vdp' ) ?></h4>
                <a class="vacature__btn" href="<?php _e( '/spontaan-solliciteren', 'vdp' ) ?>"><?php _e( 'Spontaan solliciteren', 'vdp' ) ?></a>
            </div>
        <?php } ?>
    </div>
<?php } add_shortcode( 'vacatures', 'dfib_jobs_shortcode' );

I wrote a shortcode to display all my jobs. When there are no jobs, I want to display a message instead of the available jobs. I used an else-statement to do this, but when the message shows, it shows on top of the page instead of the place where I added the shortcode.

My code:

function dfib_jobs_shortcode( $atts ) {
    ob_start();

    $query = new WP_Query( array(
        'post_type' => 'jobpost',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'orderby' => 'rand',
    ) ); ?>
    <div class="vacatures__wrapper">
        <?php if ( $query->have_posts() ) { ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="vacature__item">
                    <h4 class="vacature__title"><?php the_title() ?></h4>
                    <?php if ( has_excerpt() ) { 
                        the_excerpt();
                    } else {
                        the_content();
                    } ?>
                    <a class="vacature__btn" href="<?php the_permalink() ?>"><?php _e( 'meer info', 'vdp' ) ?></a>
                </div>
            <?php endwhile; ?>
            <div class="vacature__item spontaan">
                <h4 class="vacature__title"><?php _e( 'Spontaan solliciteren?', 'vdp' ) ?></h4>
                <a class="vacature__btn" href="<?php _e( '/spontaan-solliciteren', 'vdp' ) ?>"><?php _e( 'solliciteren', 'vdp' ) ?></a>
            </div>
            <?php wp_reset_postdata();
            $myvariable = ob_get_clean();
            return $myvariable;
        } else { ?>
            <div class="vacature__item spontaan">
                <h4 class="vacature__title"><?php _e( 'Momenteel zijn er geen vacatures!', 'vdp' ) ?></h4>
                <a class="vacature__btn" href="<?php _e( '/spontaan-solliciteren', 'vdp' ) ?>"><?php _e( 'Spontaan solliciteren', 'vdp' ) ?></a>
            </div>
        <?php } ?>
    </div>
<?php } add_shortcode( 'vacatures', 'dfib_jobs_shortcode' );
Share Improve this question edited Feb 13, 2020 at 11:00 Thessa Verbruggen asked Feb 13, 2020 at 10:52 Thessa VerbruggenThessa Verbruggen 2452 silver badges10 bronze badges 2
  • 1 Why isn't your ob_start() on the same level as ob_get_clean()? – fuxia Commented Feb 13, 2020 at 11:01
  • 1 I put it on the same level and now it works! – Thessa Verbruggen Commented Feb 13, 2020 at 11:05
Add a comment  | 

2 Answers 2

Reset to default 1

Answer

I found the answer, thanks to a comment and after some digging myself:

function dfib_jobs_shortcode( $atts ) {
ob_start();

$query = new WP_Query( array(
    'post_type' => 'jobpost',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'rand',
) );
?>
<div class="vacatures__wrapper">
<?php if ( $query->have_posts() ) { ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="vacature__item">
                <h4 class="vacature__title"><?php the_title() ?></h4>
                    <?php if ( has_excerpt() ) { 
                        the_excerpt();
                    } else {
                        the_content();
                    } ?>
                <a class="vacature__btn" href="<?php the_permalink() ?>"><?php _e( 'meer info', 'vdp' ) ?></a>
            </div>
        <?php endwhile; ?>
        <div class="vacature__item spontaan">
            <h4 class="vacature__title"><?php _e( 'Spontaan solliciteren?', 'vdp' ) ?></h4>
                <a class="vacature__btn" href="<?php _e( '/spontaan-solliciteren', 'vdp' ) ?>"><?php _e( 'solliciteren', 'vdp' ) ?></a>
        </div>
        <?php wp_reset_postdata(); ?>
<?php
} else {
    ?>
    <div class="vacature__item spontaan">
        <h4 class="vacature__title"><?php _e( 'Momenteel zijn er geen vacatures!', 'vdp' ) ?></h4>
            <a class="vacature__btn" href="<?php _e( '/spontaan-solliciteren', 'vdp' ) ?>"><?php _e( 'Spontaan solliciteren', 'vdp' ) ?></a>
    </div>
    <?php
} ?>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
add_shortcode( 'vacatures', 'dfib_jobs_shortcode' );

You can add another if around the code such as:

$total_posts = $query->found_posts;
if( $total_posts > 0 ) {
 // your code
} else {
  echo 'nothing found here';
}

found_posts is one of the properties you can view from a WP_query (more info here)

本文标签: phpShow message when query has no posts