Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Amber Hoddinott
Amber Hoddinott
6,494 Points

Can someone please explain to me where i am going wrong with my code. Would be so grateful!!!

//i am trying to get my posts to post to my template page

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

            the_content();

            endwhile;

            else :
                echo '<p>No content found</p>';

            endif; ?>

    <?php 

    wp_reset_postdata();

    $args = array (
        'category_name'=>'red'
        ) 

    $catPosts = new WP_Query($args);
    ?>


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

        <h3> <?php the_title();?> </h3>

        <?php   endwhile;

            else :
                echo '<p>No content found</p>';

            endif; ?>

3 Answers

Is the code you have here your template page? if so did you create a comment at the top to state that this is your template?

Amber Hoddinott
Amber Hoddinott
6,494 Points

Yes it is, and i had commented it at the beginning of the file to unsure this and i have a pages on my wordpress that was displaying the page. I've actually solved the problem now, thank you for your reply. My work mate and i work out that is was actually looping through my posts fine and was displaying them, however i had to add an extra wp_query parameter as we believe a filter somewhere in wordpress was affecting the number of posts being able to be displayed on the page.

is used... 'posts_per_page' => -1

...and the posts are displaying fine now :D

yup that will do it. Remember -1 shows all your posts, and if you increase the number it will show that number of posts.