Support » Plugins » Hacks » Split get_post by post_type

  • Have someone an idea how can i split a loop in post type.

    So what i want is.
    For every post type a new UL.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
    
    <ul class="post">
    <li id="post-<?php the_ID(); ?>" class="posts">        
         <?php the_title(); ?> - <?php echo get_post_type(); ?>    
       </li>
    </ul>
    
    <ul class="page">
    <li id="post-<?php the_ID(); ?>" class="posts">        
         <?php the_title(); ?> - <?php echo get_post_type(); ?>    
       </li>
    </ul>
    
    <?php endwhile; ?>
    <?php endif; ?>

    What i have right now.
    All post types in one UL.

    
    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
       <li id="post-<?php the_ID(); ?>" class="posts">        
         <?php the_title(); ?> - <?php echo get_post_type(); ?>    
       </li>
    <?php endwhile; ?>
    <?php endif; ?>
    <ul>
    query_posts(
            array(
                'posts_per_page' => -1,
                'no_found_rows' => true,
    	    'post_type' => array('post','page'),
    	    'orderby' => 'type'
            )
        );
    • This topic was modified 2 years, 7 months ago by  Messaone.
    • This topic was modified 2 years, 7 months ago by  Messaone.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Sure, you can have new ul tags for every post. You basically have it in your “what I want” example, except you don’t want a separate post and page ul class for every post. I think this is what you’re after, perhaps?

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
    
    <ul class="<?php echo get_post_type(); ?>">
       <li id="post-<?php the_ID(); ?>" class="<?php echo get_post_type(); ?>s">        
         <?php the_title(); ?> - <?php echo get_post_type(); ?>    
       </li>
    </ul>
    
    <?php endwhile; ?>
    <?php endif; ?>

    I’m not sure what you mean by “splitting the loop” though. Perhaps your intent is that all posts are in one ul group and all pages are in another ul group? The easiest way to do that would be to do two separate queries, one for all posts, another for all pages.

    To do such a grouping from a single query, you need to add a conditional to detect when the post type changes from the previous post to the current post. Start with something close to what you have now with ul tags outside the loop. Add the conditional line right after the_post().

    It’ll have logic along the line of if the last post’s post type is not the same as this post’s post type, echo out “< /ul>< ul>”. Then just before the endwhile, save the current post’s post type in a variable which will become the last post’s post type when it hits the loop’s first conditional. You’ll need to initialize this variable before the loop starts with the value of the first group’s post type or else you’ll end up with an extra, empty ul group.

    • This reply was modified 2 years, 7 months ago by  bcworkz.
Viewing 1 replies (of 1 total)
  • The topic ‘Split get_post by post_type’ is closed to new replies.