Title: Get Custom Field in Loop
Last modified: August 19, 2016

---

# Get Custom Field in Loop

 *  [allkindsofralph](https://wordpress.org/support/users/allkindsofralph/)
 * (@allkindsofralph)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/get-custom-field-in-loop/)
 * I am creating a loop to pull the posts from wordpress. I am trying to get the
   loop to get the custom field called ‘itemNum’ so that I can use that to generate
   the link to the image for that post. So far I am not having any luck. Here is
   my code:
 *     ```
       <div id="product-grid">
       		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       			<div class="product">
       				<a href="<?php the_permalink(); ?>" rel="shadowbox[<?php single_cat_title(); ?>];width=600" title="<?php the_title(); ?>">
       				<img src="<?php bloginfo('url'); ?>/images/<?php get_post_custom_values('itemNum'); ?>.jpg">
       				<p><?php the_title(); ?></p></a>
       			</div>
   
       		<?php endwhile; ?>
       		<?php endif; ?>
       </div>
       ```
   

Viewing 1 replies (of 1 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/get-custom-field-in-loop/#post-1274659)
 * Didn’t test this but try:
 *     ```
       <div id="product-grid">
       		<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
       		$itemnum = get_post_meta($post->ID, 'itemNum', true);
       		if ($itemnum) {
       		?>
   
       			<div class="product">
       				<a href="<?php the_permalink(); ?>" rel="shadowbox[<?php single_cat_title(); ?>];width=600" title="<?php the_title(); ?>">
       				<img src="<?php bloginfo('url'); ?>/images/<?php echo $itemnum; ?>.jpg">
       				<p><?php the_title(); ?></p></a>
       			</div>
   
       		<?php
       		}
       		endwhile; ?>
       		<?php endif; ?>
       </div>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Get Custom Field in Loop’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * 1 reply
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/get-custom-field-in-loop/#post-1274659)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
