当查询没有帖子时显示消息

时间:2020-02-13 作者:Thessa Verbruggen

我写了一个短代码来显示我的所有作业。当没有作业时,我想显示一条消息,而不是显示可用作业。我使用了else语句来实现这一点,但当消息显示时,它显示在页面顶部,而不是我添加短代码的位置。

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\' );

2 个回复
最合适的回答,由SO网友:Thessa Verbruggen 整理而成

Answer

我找到了答案,多亏了一条评论,在自我挖掘之后:

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\' );

SO网友:GarethTF

您可以在代码周围添加另一个if,例如:

$total_posts = $query->found_posts;
if( $total_posts > 0 ) {
 // your code
} else {
  echo \'nothing found here\';
}
found\\u posts是您可以通过WP\\u查询查看的属性之一(详细信息here)