2013-05-26 83 views
1

我不能在這裏得到這個例子工作! http://www.snilesh.com/resources/wordpress/wordpress-query_post-exclude-or-include-post-formats/WordPress的查詢排除帖子格式

我試圖得到任何不是「視頻」帖子格式的最新帖子。我不確定一旦我設置了參數,我是如何編寫查詢的,至少我不能讓它工作。這是我的代碼:

<?php 
$args = array(
    'tax_query' => array(
    array(
    'showposts' => '1', 
     'taxonomy' => 'post_format', 
     'field' => 'slug', 
     'terms' => 'post-format-video', 
     'operator' => 'NOT IN' 
    ) 
) 
); 
query_posts($args); 
?> 
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
<!--latest news--> 
<div class="latestNews"> 
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> 
<?php if (has_post_thumbnail()) : ?>   
    <a class="thumbnail" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" > 
     <?php the_post_thumbnail(); ?> 
    </a> 
<?php endif; ?> 
<?php the_excerpt(); ?> 
<a href="?cat=1">more entries</a> 
<a href="<?php the_permalink() ?>">read more</a> 
</div> 
<?php endwhile; ?> 

我不確定是否有人可以發現我在那裏做錯了什麼?會愛任何可能的幫助!

感謝

回答

4

您正在使用

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 

但是你沒有ASIGN變量中的對象,應該是

$my_query = query_posts($args); 

但是,我認爲的最好使用

$my_query = new WP_Query($args); 

所以,你可以使用

$args = array(
    'post_type' => 'post', // if the post type is post 
    'posts_per_page' => 1, 
    'tax_query' => array(
    array(
     'taxonomy' => 'post_format', 
     'field' => 'slug', 
     'terms' => 'post-format-video', 
     'operator' => 'NOT IN' 
    )) 
); 
$my_query = new WP_Query($args); 
+1

完美地工作,非常感謝您的幫助謝赫! – Desmond

+0

@Desmond,不客氣:-) –

1

解決方案:

$args = array(
    'tax_query' => array(
    array(
     'taxonomy' => 'post_format', 
     'field' => 'slug', 
     'terms' => array('post-format-video'), 
     'operator' => 'NOT IN' 
    )) 
); 
get_posts($args); 
+0

感謝您的幫助Fazle,這個工作太! – Desmond