0

I have a custom post type set up for a news blog on a site I'm building, the custom post type being blog consisting of 2 categories, blog-image-post ID=3 and blog-text-post ID=4. I want to loop each category separately as each one is displayed and styled slightly differently in the archive. I currently have this:

    <div id="blog-grid">


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

        <div class="blog-grid">
            <img class="blog-image" src="url-here" width="100" height="100" />
        </div>

<?php endwhile; endif; ?>   

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

        <div class="blog-grid-text">
            <div class="blog-grid-text-wrap">
                <?php the_title(); ?> etc etc
            </div>
        </div>

<?php endwhile; endif; ?>

    </div>

The first loop would be for blog-image-post ID=3 and the second loop for blog-test-post ID=4, I'm not sure if this is at all possible? Can't figure it out, any suggestions would be greatly appreciated!

0

Just switch conditionally. The rewind_posts() function allows you to loop through the same loop a second time, while the has_term() function allows you to check if the currently looped post has a specific term of a specific taxonomy.

if ( have_posts() )
{
    while ( have_posts() )
    {
        the_post();

        if ( ! has_term( 'term_A_slug/term_A_id', 'taxonomy' ) )
            continue;

        // display post here
    }
}
rewind_posts();

// Second loop
if ( have_posts() )
{
    while ( have_posts() )
    {
        the_post();

        if ( ! has_term( 'term_B_slug/term_B_id', 'taxonomy' ) )
            continue;

        // display post here
    }
}
  • First, jsFiddle is not for PHP :) Second, you need to use the taxonomy_name, not just taxonomy as second argument for has_term() ... hint: Look the function up in Codex for more in depth insights ;) – kaiser Jul 11 '13 at 11:55
  • yep, I was only using jsFiddle to show the code, too long for a comment :) FYI it's fixed and works great, thanks for that, appreciated! – user1374796 Jul 11 '13 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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