Title: theme building wp_query
Last modified: August 21, 2016

---

# theme building wp_query

 *  Resolved [boris16584](https://wordpress.org/support/users/boris16584/)
 * (@boris16584)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/theme-building-wp_query/)
 * hello thank for the helpers
 * i want to get the latest six posts but my code its not working for me please 
   help
    heres my code
 * $home_query = new WP_Query( ‘posts_per_page=6’ );
    $post_count = 0; if ( $home_query-
   > have_posts() ) : while ( $home_query -> have_posts() ) : $home_query -> the_post();?
   >
 * <?php $args = array(
    ‘post_type’ => ‘attachment’, ‘numberposts’ => -1, ‘post_status’
   => null, ‘post_parent’ => $post->ID );
 * $attachments = get_posts( $args );
    if ( $attachments ) { foreach ( $attachments
   as $attachment ) { $alt = trim(strip_tags( get_post_meta($attachment->ID, ‘_wp_attachment_image_alt’,
   true) )); $image_title = $attachment->post_title; $caption = $attachment->post_excerpt;
   $description = $image->post_content; $default_attr = array( ‘src’ => $src, ‘class’
   => “attachment-$size”, ‘alt’ => trim(strip_tags( get_post_meta($attachment->ID,‘
   _wp_attachment_image_alt’, true) )), );?> <?php if( !( $post_count % 3 )){ ?>
   <div class=”posts-wrapper row”> <?php } $post_count++; ?>
 * <article <?php post_class(); ?> id=”post-<?php the_ID(); ?>”>
    <div class=”post-
   thumbnail”> <?php if( has_post_thumbnail($post->ID)){?> a href=”<?php echo wp_get_attachment_url(
   $attachment->ID);?>” data-lightbox=”<?php $category = get_the_category(get_the_ID());
   if($category[0]){echo ($category[0]->cat_name);}?>” data-title=”<?php echo $image_title;?
   >”><?php echo wp_get_attachment_image( $attachment->ID, array(220,300), $default_attr);?
   > a and so on
 * im traing to build boxs of posts with thumbnails each row hes 3 boxs and i wont
   the last six it working with category but not witeout
 * $home_query = new WP_Query( ‘category_name=front-page&&posts_per_page=6’ );//
   this is working
 * thank for answers

Viewing 1 replies (of 1 total)

 *  Thread Starter [boris16584](https://wordpress.org/support/users/boris16584/)
 * (@boris16584)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/theme-building-wp_query/#post-5152699)
 * hello to all i manged to resolve my problem here the working code :
 * `$home_query = new WP_Query(‘category_name=Uncategorized&&posts_per_page=6’);
   
   $post_count = 0; if ( $home_query -> have_posts() ) : while ( $home_query -> 
   have_posts() ) : $home_query -> the_post();?> <?php if( !( $post_count % 3 )){?
   > <div class=”posts-wrapper row”> <?php }/*end if $post_count*/ $post_count++;?
   > <article <?php post_class(); ?> id=”post-<?php the_ID(); ?>”> <div class=”post-
   thumbnail”> <?php if( !(has_post_thumbnail())){?> /images/shop.png” data-lightbox
   =”<?php $category = get_the_category(get_the_ID()); if($category[0]){echo ($category[
   0]->cat_name);}?>” data-title=”NO IMAGE”><img class=”attachment-250×250 wp-post-
   image img-responsive img-thumbnail” width=”250″ height=”250″ alt=”there is no
   thumbnail for this post” src=”<?php echo get_template_directory_uri()?>/images/
   shop.png”> <?php } else{ ?> <?php $args = array(‘post_type’ => ‘attachment’, ‘
   numberposts’ => 1, ‘posts_per_page’ => 1, ‘post_status’ =>’any’, ‘post_parent’
   => $post->ID ); // ‘posts_per_page’ => -1 All posts ?> <?php $posts_ids[($post_count-
   1)] = $post->ID;?> <?php $attachments = get_posts($args); if ( $attachments ){
   foreach ( $attachments as $attachment ) { $alt = trim(strip_tags( get_post_meta(
   $attachment->ID, ‘_wp_attachment_image_alt’, true) )); $image_title = $attachment-
   >post_title; $caption = $attachment->post_excerpt; $description = $attachment-
   >post_content; $default_attr = array( ‘src’ => $src, ‘class’ => “attachment-$
   size”, ‘alt’ => trim(strip_tags( get_post_meta($attachment->ID, ‘_wp_attachment_image_alt’,
   true) )), );?> ID;?>” href=”<?php echo wp_get_attachment_url( $attachment->ID);?
   >” data-lightbox=”<?php $category = get_the_category(get_the_ID()); if($category[
   0]){echo ($category[0]->cat_name);}?>” data-title=”<?php echo $image_title;?>”
   ><?php echo wp_get_attachment_image( $attachment->ID, array(220,300), $default_attr);?
   ><?php } //end foreach } // end if ( $attachments ) } // end has_post_thumbnail?
   > <p class=”img-meta clearfix”> <em class=”date”><?php the_time(‘F jS, Y’); ?
   > </p> </div><!– end .post-thumbnail –> <div class=”post-content”> <div class
   =”entry row”> <span class=”col-xs-9″> <p class=”post-title”><?php the_title();?
   ></p> <p class=”post-tags”> <?php $tags = wp_get_post_tags(get_the_ID()); if(
   $tags) for($i=0 ; $i < count($tags) ; $i++){ if($tags[$i+1] -> name) echo $tags[
   $i] -> name . ‘ , ‘ ; else echo $tags[$i] -> name; } // end for?> </p> <?php 
   if (get_option(‘first’)){ ?> <div id=”telno”> <p>Tel: <?php echo get_option(‘
   first’); ?></p> </div> <!– end telno –> <?php } //end if get_option ?> </span
   ><!– col-xs-10 –> <span class=”col-xs-3″>” class=”btn btn-success” ><i class=”
   content content_small”></i></span> <style> #recent-post-category .btn-success
   i { background: url(“<?php echo get_template_directory_uri()?>/images/Chevron-
   right.png”) no-repeat scroll center center;} </style> </div><!– end .entry row–
   > </div><!– end .post-content –> </article> <?php if( !( $post_count % 3 )){ ?
   > </div><!– posts-wrapper row –><?php } // end if $post_count?> <?php endwhile;
   endif;?> thanks to the halpers

Viewing 1 replies (of 1 total)

The topic ‘theme building wp_query’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [posts_per_page](https://wordpress.org/support/topic-tag/posts_per_page/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [boris16584](https://wordpress.org/support/users/boris16584/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/theme-building-wp_query/#post-5152699)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
