没有内容时不显示任何内容

Don't show anything when there is no content

我在搜索结果页面中有以下代码,我希望它在任何部分都没有内容时不显示它。

            if ( have_posts() ) { 
                $types = array('singer', 'music', 'album');
                foreach( $types as $type ){
                    if( $type == 'singer'){
                        echo ' <h1>Singers</h1> ';
                    } elseif ( $type == 'music'){
                        echo '<h1>Musics</h1>';
                    }
                    elseif ( $type == 'album'){
                        echo '<h1>Albums</h1>';
                    }
                    while( have_posts() ){
                        the_post();
                        if( $type == get_post_type() ){
                            the_post_thumbnail('medium'); ?>
                            <h3><a href="<?php echo get_permalink(); ?>">
                            <?php the_title();  ?>
                            </a></h3>
                            <?php
                        }
                    }
                rewind_posts();
                }
            } else {
                echo us_translate( 'No results found.' );
            }

这不是一个好的解决方案,但应该可行)

$types = array( 
    'singer' => array( 'title' => 'Singers', 'content' => '' ), 
    'music'  => array( 'title' => 'Musics', 'content' => '' ), 
    'album'  => array( 'title' => 'Albums', 'content' => '' ),
);

if ( have_posts() ) { 

    while( have_posts() ) {
        the_post();
        $type = get_post_type();
        if ( array_key_exists( $type, $types ) ) {
            $types[ $type ][ 'content' ] .= get_the_post_thumbnail( null, 'medium' );
            $types[ $type ][ 'content' ] .= '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</h3>';
        }
    }

    foreach( $types as $k => $v ) {
        if ( $v[ 'content' ] ) {
            echo '<h1>' . $v[ 'title' ]. '</h1>' . $v[ 'content' ];
        }
    }

} else {
    echo us_translate( 'No results found.' );
}