WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to display all posts from specific categories on a single page. Therefore I edited the page.php file in my theme folder. I added an 'if-clause' to check which page currently being displayed and load all posts from the following categories.

<?php get_header(); ?>

<div id="primary">
    <div id="content" role="main">

<?php
    if (is_page(26)) {
        query_posts('cat=2,6,9,13&showposts=-1&orderby=date');    
        if (have_posts()) : 
            while (have_posts()) : 
                the_post(); 
                get_template_part( 'content', 'page' );
            endwhile; 
        endif;  
    } else {
        while ( have_posts() ) : 
            the_post(); 
            get_template_part( 'content', 'page' ); 
        endwhile; // end of the loop. 
    }
?>

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

<?php get_footer(); ?>

But when I load my page 26 nothing will be displayed.

share|improve this question
3  
Don't use query_posts wordpress.stackexchange.com/a/50762/10911. Use WP_Query for this codex.wordpress.org/Class_Reference/WP_Query – janw Jan 2 '13 at 11:47
up vote 2 down vote accepted

I'd Advise adding the arg of the category in an array. And don't use query_posts. Also showposts is deprecated use posts_per_page instead.

$args = array (
    'cat' => array(2,6,9,13),
    'posts_per_page' => -1, //showposts is deprecated
    'orderby' => 'date',
);

$cat_posts = new WP_query($args)

if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post(); 
        get_template_part( 'content', 'page' );
endwhile; endif;  
share|improve this answer

This happens because you are still using query_posts(). Stop doing that. Use WP_Query instead:

$extra_posts = new WP_Query( 'cat=2,6,9,13&showposts=-1&orderby=date' );
if ( $extra_posts->have_posts() )
{
    while( $extra_posts->have_posts() )
    {
        $extra_posts->the_post();
        get_template_part( 'content', 'page' );
    }
    wp_reset_postdata();
}
share|improve this answer

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.