简体   繁体   中英

Check if there are any Posts written with certain Post Format

I'm building a wordpress theme and I need to check if there are any Post written within certain Post Format .

This is what I have in my functions.php

add_theme_support( 'post-formats', array( 'image', 'link', 'quote', 'status', 'video', 'audio', 'gallery' ) );

And this little piece of code in page template file .

if ( current_theme_supports( 'post-formats' ) ){
    $post_formats = get_theme_support( 'post-formats' );
    if ( is_array( $post_formats[0] ) ) {
        foreach ($post_formats[0]  as $post_format) { 
            echo '<a href="#">'.$post_format.'</a>'; 
        }
    }
}

So, currently I have ALL Post Formats displayed as links. What I need is to display ONLY the ones that have Posts with that Post Format assigned.

Example:

If there are no Posts with Post Format Quote assigned, don't display quote link.

I've tried to search the web but without success. Does anyone know how to accomplish this? Thank you!

$terms = get_terms("post_format");
             $count = count($terms);
             if ( $count > 0 ){
                 echo '<ul class="list-group">';
                 foreach ( $terms as $term ) {
                  if($term->count > 0)
                    echo '<a href="#">'.$term->name.'</a>'; 
                 }
                 echo '</ul>';
             }

Try This

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM