Title: custom fields
Last modified: August 19, 2016

---

# custom fields

 *  Resolved [tara_irvine](https://wordpress.org/support/users/tara_irvine/)
 * (@tara_irvine)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/)
 * I was wondering what would be the best way to complete this..
 * I have a thumbnail custom field on every post, and they display in a div perfectly.
   However, the number of posts is going up so I want to only have 12 thumbnails
   in one div, and a link at the bottom showing another div with another 12 thumbnails
   and so on, with next and previous links included.
 * The code so far for my custom fields are ..
 *     ```
       <div id="thumbnailsbox" class="thumbnails" style="display:block;padding:0;margin:0;">
   
           <?php foreach($lastposts as $post) : setup_postdata($post);?>
   
        <?php $imgThumb = get_post_meta($post->ID,'img_thumb', true); ?>
   
           <?php if($imgThumb !=""){ ?>
   
           <a>" title="<?php the_title(); ?>"><?php echo "<img class='thumb' src='$imgThumb'/>" ?></a>
   
           <?php } else { echo ''; } ?>
           <?php endforeach; ?>
           <?php endwhile; else: ?>
           <?php _e('Sorry, no posts matched your criteria.'); ?>
           <?php endif; ?>
   
         </div>
         <!-- close thumbnails-->
       ```
   
 * I’ve been struggling with this for a while, and I’d really like to nail this,
   so any help would be awesome!
 * xo

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

 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591572)
 * maybe (I don’t know, I’m a noob =p ) you should use [ posts_nav_links ](http://codex.wordpress.org/Function_Reference/posts_nav_link)
 *  Thread Starter [tara_irvine](https://wordpress.org/support/users/tara_irvine/)
 * (@tara_irvine)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591592)
 * I don’t know how to stop displaying the thumbnails after a certain number, say
   12.
 * I just tried to do this…
 *     ```
       <div id="thumbnailsbox" class="thumbnails" style="padding:0;margin:0;">
   
           <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
           <?php  $lastposts = get_posts('numberposts=12&category=5');?>
           <?php foreach($lastposts as $post) : setup_postdata($post);?>
   
           <?php $imgThumb = get_post_meta($post->ID,'img_thumb', true); ?>
   
           <?php if($imgThumb !=""){ ?>
   
           <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo "<img class='thumb' src='$imgThumb'/>" ?></a>
   
           <?php } else { echo ''; } ?>
           <?php endforeach; ?>
           <?php endwhile; else: ?>
           <?php _e('Sorry, no posts matched your criteria.'); ?>
           <?php endif; ?>
   
           <a href="#" id="nextimgs">Next</a>
   
         </div>
         <!-- close thumbnails--> 
   
       <div id="thumbnailsbox2" class="thumbnails" style="display:none;padding:0;margin:0;">
   
           <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
           <?php  $lastposts2 = get_posts('numberposts=12&category=5');?>
           <?php foreach($lastposts2 as $post) : setup_postdata($post);?>
   
           <?php $imgThumb = get_post_meta($post->ID,'img_thumb', true); ?>
   
           <?php if($imgThumb !=""){ ?>
   
           <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo "<img class='thumb' src='$imgThumb'/>" ?></a>
   
           <?php } else { echo ''; } ?>
           <?php endforeach; ?>
           <?php endwhile; else: ?>
           <?php _e('Sorry, no posts matched your criteria.'); ?>
           <?php endif; ?>
   
           <a href="#" id="previmgs">Previous</a>
   
        </div>
         <!-- close thumbnails--> 
   
          <script type="text/javascript">
   
       $('#nextimgs').click(function(){
       		$('#thumbnailsbox').hide();
       		$('#thumbnailsbox2').fadeIn();
       	});
   
       $('#previmgs').click(function(){
       		$('#thumbnailsbox2').hide();
       		$('#thumbnailsbox').fadeIn();
       	});
   
       	</script>
       ```
   
 * this is the way I want it to work, but the second div should display the NEXT
   set of thumbnail images.
 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591814)
 * not a nice solution, but you could use something like that :
 *     ```
       <div id="thumbnailsbox2" class="thumbnails" style="display:none;padding:0;margin:0;">
           <?php $count = 24; ?>
           <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
           <?php  $lastposts2 = get_posts('numberposts=24&category=5');?>
           <?php foreach($lastposts2 as $post) : setup_postdata($post);?>
           <?php if($count < 13){
           <?php $imgThumb = get_post_meta($post->ID,'img_thumb', true); ?>
   
           <?php if($imgThumb !=""){ ?>
   
           <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo "<img class='thumb' src='$imgThumb'/>" ?></a>
   
           <?php } else { echo ''; } ?>
           <?php    } $count--; ?>
           <?php endforeach; ?>
           <?php endwhile; else: ?>
           <?php _e('Sorry, no posts matched your criteria.'); ?>
           <?php endif; ?>
   
           <a href="#" id="previmgs">Previous</a>
   
        </div>
       ```
   
 * I think there is a better solution !
 *  Thread Starter [tara_irvine](https://wordpress.org/support/users/tara_irvine/)
 * (@tara_irvine)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591822)
 * it works! thank you so much, I know what you mean about a better solution, like
   what if there are more than 24 custom thumbnails.. Your solution will work tho,
   so thank-you very much 🙂
    xo
 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591825)
 * good 🙂
    (put the topic on “resolved”)
 *  Thread Starter [tara_irvine](https://wordpress.org/support/users/tara_irvine/)
 * (@tara_irvine)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591826)
 * well i have a question first, what if there’s only 4 or 5 images for one category?
   the second div is empty but still the next and previous links work.. sorry to
   be a pain.
 *  [forunner](https://wordpress.org/support/users/forunner/)
 * (@forunner)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591828)
 * hum inverse the count variable
 *     ```
       <div id="thumbnailsbox2" class="thumbnails" style="display:none;padding:0;margin:0;">
       $count = 0;
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
           <?php  $lastposts2 = get_posts('numberposts=24&category=5');?>
           <?php foreach($lastposts2 as $post) : setup_postdata($post);?>
           <?php if($count > 12){
           <?php $imgThumb = get_post_meta($post->ID,'img_thumb', true); ?>
   
           <?php if($imgThumb !=""){ ?>
   
           <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo "<img class='thumb' src='$imgThumb'/>" ?></a>
   
           <?php } else { echo ''; } ?>
           <?php    } $count++; ?>
           <?php endforeach; ?>
           <?php endwhile; else: ?>
           <?php _e('Sorry, no posts matched your criteria.'); ?>
           <?php endif; ?>
   
           <a href="#" id="previmgs">Previous</a>
   
        </div>
       ```
   
 * and put a condition on your links …something like if(count > 12)
 * [humm, I don’t know if it will work]
 *  Thread Starter [tara_irvine](https://wordpress.org/support/users/tara_irvine/)
 * (@tara_irvine)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591837)
 * what worked was I added a $i ==0 then $i ++ after the img was called so it counted
   the number on the screen.. after that i added a php loop calling the next link
   when $i >11. works well, thanks for all your help!

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

The topic ‘custom fields’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [tara_irvine](https://wordpress.org/support/users/tara_irvine/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/custom-fields-23/#post-1591837)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
