0

I am trying to write a custom post query for a specific post type in a way where only first 2 thumb/featured-image will be shown & other will be just title & other metadata (i.e the picture in attachment). It would be great if someone help me regarding this issue with proper guideline. Thanks in advance.

attachment

0

Let's go over how this works so that next time you face this problem or a similar one, you have an idea of how to resolve it. This is expanding more on Ben's answer, the comments are in the code explaining what's happening. And you would replace "my-custom-type" with whatever your custom post type is.

You can find more about which queries are best to use for what purpose here as well: http://codex.wordpress.org/Function_Reference/query_posts

/* First: Using WP_Query to create your own custom loop with 2 posts starting from the first post from your custom post type */ 
$custom_loop = new WP_Query(array( 'posts_per_page' => 2, 'post_type' => 'my-custom-type' ));

/* Starting the first loop! */
while ( $custom_loop->have_posts() ) : $custom_loop->the_post(); 

    /* The post thumbnail or whatever you'd like */
    the_post_thumbnail();
    the_content();

endwhile; wp_reset_postdata(); 
/* Close this loop and don't forget to reset the query with wp_reset_postdata() */

/* Second: Now using WP_Query to create your second loop that takes off from the third post from your custom post type using offset, this one has 4 posts like your example */ 
$custom_loop_two = new WP_Query(array( 'posts_per_page' => 4, 'post_type' => 'my-custom-type', 'offset' => '2' ));

/* Starting the second loop! */
while ( $custom_loop_two->have_posts() ) : $custom_loop_two->the_post(); 

    /* Just the content - no post thumbnail or whatever you'd like */
    the_content();

endwhile; wp_reset_postdata(); 
/* Close this loop and don't forget to reset the query with wp_reset_postdata() */

Hope that helps! :-)

| improve this answer | |
  • Thanks a lot @RachieVee. Now I understand where was my fault was. :) Great help. – Sumon May 20 '14 at 19:38
1

I would run two loops here the first one which gets the image and text and then a second loop for just the heading and text but offsetting the query by 2!

The first one like this

$query = new WP_Query( array( 'posts_per_page' => 2, 'cat' => 3, ) ); 

and second like this:

 $query = new WP_Query( array( 'posts_per_page' => 4,  'cat' => 3, ,'offset' => 2 ) );

If this doesn't make sense let me know and I will write the code for you :)

| improve this answer | |
0

There's no need for multiple queries, check where you are in the loop via the current_post var:

$args = array( 'posts_per_page' => 6 );
$loop = new WP_Query( $args );

while( $loop->have_posts() ){
    $loop->the_post();

    if( $loop->current_post < 2 ){
        // first two posts,
        // output thumb, excerpt, etc..
    } else {
        // post 3+
        // output just title and meta
    }
}
| improve this answer | |
  • Ooh I've never tried it this way before, will keep this in mind for myself next time. As for the two queries, I feel like it's easier to wrap two separate loops with HTML and style them with CSS if I need to or in your example - would you just insert your HTML within/around your if/else statement? – RachieVee May 20 '14 at 17:43
  • you could do that. you can also manipulate the loop by setting the current post like this, and calling break to exit a loop before all posts are output. – Milo May 20 '14 at 17:47
  • Ah okay, the break gets out of the if statement, correct? And when using this method - is it no longer necessary to reset/clear the loop at the end? – RachieVee May 20 '14 at 19:32

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.