8

I'm using WP 4.0 and Headway as my theme. I've created a custom post type called 'property' and am looking to create the loop in 2 columns. but I'm not sure what to add to the following code to make it happen. I'd also like to add pagination.

I also have other info to place inside the loop. just working on getting it working right.

Here's the code that's currently working for me... without columns.

<?php $loop = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => -1, 'category' => 'current' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="pindex">
    <div class="pimage">
        <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?></a>
    </div>
    <div class="ptitle">
        <h2><?php echo get_the_title(); ?></h2>
    </div>
</div>
<?php endwhile; wp_reset_query(); ?>
3
  • 1
    Sorry, but could you please read How to Ask and add edits to your post instead of comments? Make sure you read the help center to find your way around the site. There are also WYSIWYG buttons for the editor to format your questions contents.
    – kaiser
    Oct 19, 2014 at 2:00
  • Your question is off topic as it relates to the modification of a commercial theme. Why can't you ask this question on the Headway theme forums? Oct 19, 2014 at 6:57
  • A commercial theme which isn't publicly accessible. Oct 19, 2014 at 7:04

1 Answer 1

12

There are couple of issues in your query.

  1. There is not parameter named category. You can use these following.

    cat (int) - use category id.
    category_name (string) - use category slug (NOT name).
    category__and (array) - use category id.
    category__in (array) - use category id.
    category__not_in (array) - use category id.
    
  2. If you need your query to paginate then you should not use posts_per_page' => -1. This will overwrite pagination and return all posts.

  3. One more thing, you are checking for post thumbnail at wrong place. You should check it before the image container.

So I have modified your query and this is how it will be. I am assuming your category slug is current as you used in your query.

<?php
    $loop = new WP_Query( array( 'post_type' => 'property', 'category_name' => 'current', 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="pindex">
                <?php if ( has_post_thumbnail() ) { ?>
                    <div class="pimage">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                <?php } ?>
                <div class="ptitle">
                    <h2><?php echo get_the_title(); ?></h2>
                </div>
            </div>
        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
            </div>
        <?php endif;
    endif;
    wp_reset_postdata();
?>

This should return all posts in category current with pagination. If you need to get posts from more than one category then you can use category__in parameter instead of category_name.

'category__in' => array( 2, 6 )

Note that category__in accpepts only category IDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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