【问题标题】:Check if there are any Posts written with certain Post Format检查是否有任何以某种帖子格式编写的帖子
【发布时间】:2013-10-15 15:05:26
【问题描述】:

我正在构建一个 wordpress 主题,我需要检查是否有任何帖子以某种帖子格式编写

这就是我的 functions.php

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

页面模板文件中的这一小段代码。

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>'; 
        }
    }
}

所以,目前我将所有帖子格式显示为链接。我需要的是ONLY显示分配了该帖子格式的帖子。

示例:

如果没有指定帖子格式引用的帖子,则不要显示引用链接。

我尝试搜索网络但没有成功。有谁知道如何做到这一点?谢谢!

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:
    $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>';
                 }
    

    试试这个

    【讨论】:

    • 正是我需要的!非常感谢@wordpresser !!它就像一个魅力!抱歉,我没有足够的声誉来投票支持您的答案,但我将其标记为已接受。
    • lol 它的 okey .. 很高兴它有帮助。 post-format 也是分类法:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多