我试图在这个页面上只发布来自“Fotos Abertas”类别的帖子,但我没有得到,显示的是所有类别。我的模板的代码中是否有任何错误?
<?php
/*
* Template Name:Modelos Fechadas
*/
get_header(); ?>
<div class="container">
<div class="header-field">
<div class="page-title pull-left">
<h3><?php the_title(); ?></h3>
</div>
</div><!-- /.header-field -->
</div><!-- /.container -->
<div class="container blog">
<div class="cont-outer">
<div class="row-fluid">
<?php query_posts(\'category_name=Fotos Fechadas\'); ?>
<?php
$loop = new WP_Query( array(\'post_type\' => \'post\',\'paged\' => $paged) );
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
global $post,$wp_query;
at_set_query($loop);
// $meta = get_post_meta( $post->ID, $key = \'staff_meta\', true );
?>
<div class="span6">
<div id="post-<?php the_ID(); ?>" <?php post_class(\'post-item\'); ?>>
<?php if(has_post_thumbnail( )) { ?>
<div class="post-image">
<?php the_post_thumbnail( \'blog-post\' ); ?>
<a href="<?php the_permalink(); ?>" target="_blank" class="more"><?php _e(\'Veja mais sobre a modelo\',\'at\'); ?></a>
<div class="post-info">
<ul>
<li>
<?php the_title(); ?>
</li>
</ul>
</div>
</div>
<?php } ?>
</div>
</div><!-- /.span8 -->
<?php endwhile; ?>
<div class="pagination">
<?php echo at_pagination(array( \'type\' => \'numbers\', \'class\' => \'\'));
at_restore_query();
?>
</div>
<?php endif; ?>
</div>
<!-- /.row-fluid -->
</div><!-- /.cont-outer -->
</div><!-- /.container -->
<div id="bg_slide">
<ul>
<li><img src="<?php bloginfo(\'stylesheet_directory\'); ?>/img/bg_wall.jpg" /></li>
</ul>
<div class="pattern"></div>
</div>
<?php get_footer(); ?>
最合适的回答,由SO网友:Twifty 整理而成
根据codex query_posts
注意:此功能不适用于插件或主题。如后文所述,有更好、性能更好的选项来更改主查询。双重注意:query\\u posts()是一种过于简单且有问题的方法,通过将页面的主查询替换为新的查询实例来修改它。它效率低下(重新运行SQL查询),并且在某些情况下会彻底失败(尤其是在处理POST分页时)。任何现代的WP代码都应该使用更可靠的方法,比如使用pre\\u get\\u posts钩子。TL;DR从不使用query\\u posts();
您应该将类别传递给wp_query
$args = array(
\'post_type\' => \'post\',
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => \'fotos-fechadas\'
// Or with the ID
\'field\' => \'id\',
\'terms\' => 19
)
)
);
$loop = new WP_Query( $args );