Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Gabriel Ward
Gabriel Ward
20,222 Points

displaying particular posts in Wordpress

I'm having a go at building a Wordpress theme from scratch. I want to have two columns and display the first half of my posts in the first column, and the second half of my posts in the second column. Here's my code in index.php for the first column

<?php get_header(); ?>


    <div class="grid-container">
        <div class="grid-5">
            <!-- Start the Loop. -->
 <!-- Get the last 5 posts in the special_cat category. -->
<?php query_posts( 'category_name=first_cat&posts_per_page=5' ); ?>

<?php while ( have_posts() ) : the_post(); ?>
    <!-- Do special_cat stuff... -->
<?php endwhile; ?>
    </div>


<?php get_footer(); ?>

I have given the the posts the category of 'first'. Nothing is showing up. Any tips are greatly appreciated. I'm not worried about using the category method in particular. I'm just wanting a way to select specific posts and display them.

1 Answer

Andrew McCormick
Andrew McCormick
17,730 Points

have you tried with that category ID instead of slug?

Gabriel Ward
Gabriel Ward
20,222 Points

No I haven't, how would I do that? I'm feeling a bit confused because there are so many options.

I've also tried the following code. It works, but the only thing it's deprecated and I can't yet work out how to do this using up to date code:

<?php get_header(); ?>


    <div class="grid-container">
        <div class="grid-5">
            // FIRST LOOP: display posts 1 thru 5
            <?php query_posts('showposts=5'); ?>
            <?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
            <?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>

            <?php the_title(); ?>
            <?php the_content(); ?>

            <?php $count1++; } ?>
            <?php endforeach; ?>
        </div>
        <div class="grid-5">
        // SECOND LOOP: display posts 6 thru 10
            <?php query_posts('showposts=5'); ?>
            <?php $posts = get_posts('numberposts=5&offset=5'); foreach ($posts as $post) : start_wp(); ?>
            <?php static $count2 = 0; if ($count2 == "5") { break; } else { ?>

            <?php the_title(); ?>
            <?php the_content(); ?>

            <?php $count2++; } ?>
            <?php endforeach; ?>
        </div>

<?php get_footer(); ?>
Andrew McCormick
Andrew McCormick
17,730 Points

on second thought, you need to pass the name in an array. try this..

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );

http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category

Gabriel Ward
Gabriel Ward
20,222 Points

Cool ok that seems to be working. Say I have ten posts and I want to display them in two columns. First five in the first column and second five in the second column. How would I do this in terms of selecting post numbers, and not having to rely on categories?

Thanks so much for your help, it's greatly appreciated.

Gabriel Ward
Gabriel Ward
20,222 Points

One more thing I'm trying to figure out regarding columns like this. I would like to display my about page so that the text is in two columns, so it looks like the following page http://letterbox-mag.com/about.html. Do you know how would I select the first half of the content in one loop, and then the second half of the content in the second loop? Thanks so much for any help.

Andrew McCormick
Andrew McCormick
17,730 Points

The immediate way that I think to do that would be to use a secondary query. In your main query you might pull 5 posts and then create a secondary query (http://codex.wordpress.org/Function_Reference/query_posts#Secondary_Loops) where you would add an offset to your query to skip the 5 posts that you pulled in your first query. see : http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination .
I'm sure there is a more elegant way to do that, but that's what comes to my mind.