2011-09-20 58 views
2

我最近添加了我的WordPress主題的文章格式 - 在博客頁面的罰款,因爲他們都是相應的樣式。然而,在我的主頁模板上,我只想顯示'標準'帖子格式(沒有鏈接,畫廊,音頻,視頻等)。只在模板上顯示WordPress'標準'文章格式?

在我的主題選項中,我可以決定在頭版顯示多少帖子,這是'dft_recent_number'的用途。

有誰知道我可以如何更改下面的代碼來排除除'標準'後的所有格式?

<?php 
$query = new WP_Query(); 
    $query->query('posts_per_page='.get_option('dft_recent_number')); 

    //Get the total amount of posts 
$post_count = $query->post_count; 

    while ($query->have_posts()) : $query->the_post(); 

?> 

任何幫助非常感謝!

回答

4

WP_Query似乎沒有一個簡單的參數爲post_format

從我的快速研究看來,後期格式通過分類法相關。所以,理論上,使用分類參數應該工作。

$args = array(
    'tax_query' => array(
     array(
      'taxonomy' => 'post_format', 
      'field' => 'slug', 
      'terms' => 'post-format-standard', 
     ) 
    ) 
); 
$query = new WP_Query($args); 

注:你需要更新你的博客的分類名稱和蛞蝓。這應該是你在你的functions.php文件中設置的名字。

+1

這讓我在正確的方向傑森,謝謝! 我使用的 '不在' 以除去格式我不想例如 '的$ args =陣列( \t 'showposts'=> $數, \t 'tax_query'=>數組( \t \t \t陣列( '分類'=> 'post_format', \t \t \t \t '字段'=> '蛞蝓', \t \t \t \t '術語'=>數組( '後格式的視頻',「 - 格式 - 交報價','後格式鏈接','後格式音頻'), \t \t \t \t '運算符'=> 'NOT IN' \t \t \t \t) \t \t \t) \t); \t query_posts($ args);' – Justin

+0

不夠公平。很高興它的工作。 –

4
// there's no post-format-standard so you should write it like this to exclude all other postpformats 
array(
'taxonomy' => 'post_format', 
'field' => 'slug', 
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'), 
'operator' => 'NOT IN' 
) 
1

我知道這是老了,但我面臨着同樣的問題,儘管我發現我在想一個解決方案都做什麼其他的「修理」這一點。

我覺得更靈活的解決方案可能是類似的東西:

$post_formats = get_theme_support('post-formats'); 

$tax_query = false; 
if ($post_formats) { 
    $tax_query = array(
     array(
      'taxonomy' => 'post_format', 
      'field' => 'slug', 
      'terms' => $post_formats[0], 
      'operator' => 'NOT IN' 
     ) 
    ); 
} 

// WP_Query arguments 
$args = array(
    'post_type' => 'post', 
    'order'  => 'DESC', 
    'orderby' => 'date', 
    'tax_query' => $tax_query 
); 

這將排除已啓用,也將在案件WP工作,將增加更多的崗位格式後的格式(或增加的能力添加更多)。

相關問題