Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to output all the posts of a certain category but all that is happening is The page content is being outputted no posts?

template page

<?php
/*
Template Name: Blog
*/
?>

<?php get_header(); ?>

            <div id="content">

                <div id="inner-content" class="wrap clearfix">

                    <div id="main" class="eightcol first clearfix" role="main">



                   <?php $query = new WP_Query( 'category_name=blog' ); ?> 


                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                        <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">

                            <header class="article-header">

                                <h1 class="page-title"><?php the_title(); ?></h1>
                  <p class="byline vcard"><?php
                    printf(__('Posted <time class="updated" datetime="%1$s" pubdate>%2$s</time> by <span class="author">%3$s</span>.', 'bonestheme'), get_the_time('Y-m-j'), get_the_time(__('F jS, Y', 'bonestheme')), bones_get_the_author_posts_link());
                  ?></p>


                            </header> <!-- end article header -->

                            <section class="entry-content">
                                <?php the_content(); ?>
                            </section> <!-- end article section -->

                            <footer class="article-footer">
                                <p class="clearfix"><?php the_tags('<span class="tags">' . __('Tags:', 'bonestheme') . '</span> ', ', ', ''); ?></p>

                            </footer> <!-- end article footer -->

                            <?php comments_template(); ?>

                        </article> <!-- end article -->

                        <?php endwhile; ?>  

                        <?php else : ?>

                            <article id="post-not-found" class="hentry clearfix">
                                <header class="article-header">
                                    <h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
                                </header>
                                <section class="entry-content">
                                    <p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
                                </section>
                                <footer class="article-footer">
                                    <p><?php _e("This is the error message in the page-custom.php template.", "bonestheme"); ?></p>
                                </footer>
                            </article>

                        <?php endif; ?>


                    </div> <!-- end #main -->

                    <?php get_sidebar(); ?>

                </div> <!-- end #inner-content -->

            </div> <!-- end #content -->

<?php get_footer(); ?>
share|improve this question
    
You're using WP_Query incorrectly. See my answer for details. –  bungeshea Feb 1 '13 at 7:56
add comment

1 Answer 1

You are using WP_Query incorrectly: you need to be calling the have_posts() and the_post() functions as methods of your $query variable:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Becomes:

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

You should also call wp_reset_postdata(); after the query loop is finished.

Learn more about how to use the WP_Query class at the WordPress Codex.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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