获取帖子格式

时间:2013-03-14 11:25:14

标签: php wordpress

我需要使用类别名称作为H1的类,并且还需要排除类别2.所以我将下面的PHP代码混为一谈。它工作正常,除了帖子失去它的格式。我知道get_post_format()适用于格式,但是当我添加它时,似乎没有做任何事情。任何帮助将不胜感激,谢谢。

<?php while ( have_posts() ) : the_post();
    $excludedcats = array(2);
    $count = 0;
    $categories = get_the_category();
    foreach($categories as $category)
    {
        $count++;
        if ( !in_array($category->cat_ID, $excludedcats) )
            {
             echo '<h1 class="category-heading ' . sprintf( __( "heading-%s" ), $category->slug ) . '" >';

             if( $count != count($categories) )
                 {
            echo " ";
             }

        }
    } 
single_post_title();
echo '</h1>';
the_content(); 
?>

1 个答案:

答案 0 :(得分:0)

get_post_format()不适用格式;它只返回帖子类型,然后您可以使用它来应用格式。为了使不同的帖子类型具有不同的格式,您可以执行以下操作:

<?php
    get_template_part( 'content', get_post_format() );
?>

然后您需要另一个文件content-[post-type-slug].php(例如,对于图片帖子,该文件将被称为content-image.php),您可以在其中显示帖子。你也可以在get_post_format()的返回值上使用switch语句,但这可能会变得混乱。