I'm trying to post on this page only posts from category "Fotos Abertas" but I'm not getting, is showing all categories. Is there any error in the code of my template?

* 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><!-- /.header-field -->
        </div><!-- /.container -->

        <div class="container blog">
            <div class="cont-outer">
                <div class="row-fluid">

                    <?php query_posts('category_name=Fotos Fechadas'); ?>

                        $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;

                        // $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">
                                            <?php the_title(); ?>
                            <?php } ?>

                        </div><!-- /.span8 -->
                <?php endwhile; ?>
                <div class="pagination">
                <?php echo at_pagination(array( 'type' => 'numbers', 'class' => ''));

                <?php endif; ?>

                <!-- /.row-fluid -->
            </div><!-- /.cont-outer -->
        </div><!-- /.container -->

        <div id="bg_slide">
            <li><img src="<?php bloginfo('stylesheet_directory'); ?>/img/bg_wall.jpg" /></li>
            <div class="pattern"></div>
<?php get_footer(); ?>

As per the codex query_posts

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. Double Note: query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose. TL;DR don't use query_posts() ever;

You should instead pass the category to wp_query

$args = array(
    'post_type' => 'post',
    'paged' => $paged,
    'tax_query' => array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => 'fotos-fechadas'
    // Or with the ID
            'field' => 'id',
            'terms' => 19
$loop = new WP_Query( $args );
  • this is not working :/ is showing all categories – Chead Oct 9 '13 at 18:58
  • @Chead Are you sure the category name exists? We can try it with the categories slug or ID if you'd like to post them. – Twifty Oct 9 '13 at 19:03
  • slug = fotos-fechadas ID = 19 – Chead Oct 9 '13 at 19:06
  • @Chead Welcome, as an after though, It may be better to use the ID since it would avoid comparing strings. Slightly faster. Added to code above. – Twifty Oct 9 '13 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.