Title: Need help for query / php
Last modified: August 19, 2016

---

# Need help for query / php

 *  Resolved [xinfo](https://wordpress.org/support/users/xinfo/)
 * (@xinfo)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/)
 * Hi guys
 * i have a problem in buliding query
 * problem :it shows post image and default image
 * i want to show post image if not available, default image
    i have placed the 
   code and function for thumbnail which i used
 *  **code **
 *     ```
       <div class="newssection">
       					<div class="heading">
       						<h3>Latest Posts in <a href="'.$cat_link.'" title="'.$cat_name.'">'.$cat_name.'</a> category</h3>
       						<div class="clear"></div>
       					</div>
       							global $post;
       							$myposts = get_posts("numberposts=1&category=$cat_number");
       							foreach($myposts as $post)
       								{
       									$posttitle = get_the_title();
       									$posttime = get_the_time('F jS, Y');
       									$postauthor = get_the_author();
       									$postlink = get_permalink();
       									$text = get_the_excerpt();
       									$text = substr($text , 0, 150);
       									$nothumb = get_bloginfo('template_directory');
       									 $thumb = dp_attachment_image($post->ID, "Thumbnail", true);  
   
       									echo '<div class="article">';
       										echo '<div class="left">';
       											echo '<img src="';
       												if ($thumb == NULL) { echo ''.$nothumb.'/images/defaultthumb.jpg'; } else { echo $thumb; }
       											echo '" alt="'.$posttitle.'" />';
       											echo $posttime.'<br />';
       											echo $postauthor;
       										echo '</div>';
       ```
   
 * **function for thumb **
 *     ```
       # Displays post image attachment (sizes: thumbnail, medium, full)
       function dp_attachment_image($postid=0, $size='thumbnail', $attributes='') {
       	if ($postid<1) $postid = get_the_ID();
       	if ($images = get_children(array(
       		'post_parent' => $postid,
       		'post_type' => 'attachment',
       		'numberposts' => 1,
       		'post_mime_type' => 'image',)))
       		foreach($images as $image) {
       			$attachment=wp_get_attachment_image_src($image->ID, $size);
       			?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php
       		}
       }
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Eric Mann](https://wordpress.org/support/users/ericmann/)
 * (@ericmann)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158018)
 * Try changing this line:
 * `if ($thumb == NULL) { echo ''.$nothumb.'/images/defaultthumb.jpg'; } else { 
   echo $thumb; }`
 * To this:
 * `if (! $thumb) { echo ' ' . $nothumb . '/images/defaultthumb.jpg'; } else { echo
   $thumb; }`
 *  Thread Starter [xinfo](https://wordpress.org/support/users/xinfo/)
 * (@xinfo)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158151)
 * this solution didn’t solve the problem it show’s post image and black space for
   the image
 *  [Eric Mann](https://wordpress.org/support/users/ericmann/)
 * (@ericmann)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158161)
 * One more thing to change, in your dp_attachment_image() function. Change:
 *     ```
       if ($images = get_children(array(
       		'post_parent' => $postid,
       		'post_type' => 'attachment',
       		'numberposts' => 1,
       		'post_mime_type' => 'image',)))
       		foreach($images as $image) {
       			$attachment=wp_get_attachment_image_src($image->ID, $size);
       			?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php
       		}
       }
       ```
   
 * To this:
 *     ```
       $images = get_children(array(
       		'post_parent' => $postid,
       		'post_type' => 'attachment',
       		'numberposts' => 1,
       		'post_mime_type' => 'image'));
       if ($images)
       		foreach($images as $image) {
       			$attachment=wp_get_attachment_image_src($image->ID, $size);
       			?><img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> /><?php
       		}
       }
       ```
   
 *  Thread Starter [xinfo](https://wordpress.org/support/users/xinfo/)
 * (@xinfo)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158171)
 * ericmann this solution work great thank you so much
 * well i like to know how to echo height and width in the image using aboove function
   currently it print
 * <?php echo $attachment[0]; ?>” <?php echo $attributes; ?> />
 * i want to show the image height and width in the source i hope you under stand
 *  [Eric Mann](https://wordpress.org/support/users/ericmann/)
 * (@ericmann)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158181)
 * Unfortunately that is something I don’t know how to do. I can take a look and
   try a few things, but it will be a few days before I can get back to you with
   anything.
 *  Thread Starter [xinfo](https://wordpress.org/support/users/xinfo/)
 * (@xinfo)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158188)
 * ok thak you so much for your effort to help me out well i will be waiting for
   your reply you post it here if you find any solution
 * Thanks Again
 *  [Eric Mann](https://wordpress.org/support/users/ericmann/)
 * (@ericmann)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158197)
 * OK, I’ve got it. Replace the following:
 * `<img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />`
 * With:
 * `<img src="<?php echo $attachment[0]; ?>" width="<?php echo $attachment[1]; ?
   >" height="<?php echo $attachment[2]; ?>" />`
 * According to what’s in the WordPress documentation for the wp_get_attachment_image_src()
   function, that should work. Let me know if it helps!
 *  Thread Starter [xinfo](https://wordpress.org/support/users/xinfo/)
 * (@xinfo)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158203)
 * ericmann
 * this work work very well thank you for taking your time to help me out i am making
   as resolved this topic thank you once again

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Need help for query / php’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [xinfo](https://wordpress.org/support/users/xinfo/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/need-help-for-query-php/#post-1158203)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
