2013-05-26 43 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