2013-03-14 37 views
0

我需要使用類別名稱作爲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(); 
?> 

回答

0

get_post_format()不適用格式;它只是返回帖子類型,然後您可以使用它來應用該格式。爲了對不同的職位類型不同的格式,你可以做這樣的事情:

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

然後你需要另一個文件,content-[post-type-slug].php(如圖像的帖子,該文件將被稱爲content-image.php),您顯示帖子。您也可以在get_post_format()的返回值上使用switch語句,但可能會變得混亂。