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

Displaying custom post types in two columns in Wordpress

Hi,

I’m building my own Wordpress theme whilst following along with the Treehouse tutorials. (Please post more soon!)

What I’m trying to do is display a custom post type (“news”) in two columns on the front-page template. I found a

n article online that explains how to use PHP’s modulus operator to display two loops with the rewind posts function, however I can’t seem to make it work with custom posts – I just get a syntax error. Any suggestions?

Here’s my Code:

<?php    
    $args = array(     
  'post_type' => 'news'    
     );     
    $the_query = new WP_Query( $args );     
?>    

<?php if ( have_posts() ) : while( $the_query->have_posts() ) : $i++; if( ($i % 2) == 0 ) : $the_query->next_post(); else : $the_query->the_post(); ?>

left column

<?php endif; endwhile; else: ?>    
Alternate content    
<?php endif; ?>    

<?php $i = 0; rewind_posts(); ?>

<?php if ( have_posts() ) : while( $the_query->have_posts() ) : $i++; if( ($i % 2) !== 0 ) : $the_query->next_post(); else : $the_query->the_post(); ?>    

right column

<?php endif; endwhile; else: ?>    
Alternate content    
<?php endif; ?>

7 Answers

John Locke
John Locke
15,479 Points

Do you have a link to the article that you're referencing?

John Locke
John Locke
15,479 Points

Everything seems correct. What I would do is open your template in a code editor and reference the line number that you are getting a syntax error on, and see if something is out of place.

I go through this all the time. Sometimes I haven't closed something properly, or a bracket or semicolon is out of place. A lot of time, that's all it is.

Thanks I'll do that.

John Locke
John Locke
15,479 Points

Let me know if you get it to work, and what the bug was if you do.

I re-copied and pasted the code from the article into my template and now it seems to work fine. It must've been that I didn't close something correctly. Thanks for your help!