Title: Advice on Code foreach
Last modified: August 19, 2016

---

# Advice on Code foreach

 *  Resolved [deeleea](https://wordpress.org/support/users/deeleea/)
 * (@deeleea)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/advice-on-code-foreach/)
 * Hi, I’m building my first WP theme and to date have figured out a lot of what
   I need but I am a bit stuck… I’m trying to create a catalogue page that aggregates
   individual item/product pages. I’ve succeeded to a point as can be seen on the
   live staging site here ([http://acande.as-scene.net.au/product-catalogue/mini-balun/)](http://acande.as-scene.net.au/product-catalogue/mini-balun/))
   but I want to add some other variables to the loop and that’s where I get stuck…
 * Here’s the code that achieves the above.
 *     ```
       <?php
       	$pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
       	$count = 0;
       	foreach($pages as $page)
       	{
       	?>
       <div class="thumbnail">
       <a href="<?php echo get_page_link($page->ID) ?>"><img src="<?php $upload_dir = wp_upload_dir(); ?><?php echo $upload_dir['baseurl']; ?>/B04002060-65-140x14	0.jpg" width="140" height="140"></a><br /><a href="<?php $upload_dir = wp_upload_dir(); ?><?php echo $upload_dir['baseurl']; ?>/B04002060-65.pdf" target="_blank" title="Mini Balun 1.6/5.6 Male Screw  to suit 0.5-0.65mm STP/UTP 75/120 Ohm 2/8/34 Mbit/s"><img src="<?php bloginfo('template_directory'); ?>/images/pdf-icon-small.gif" width="16" height="16" alt="Link to PDF document" /></a><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
   
       </div>
       	<?php
       	}
       ?>
       ```
   
 * There are still some elements of the html output that I want to be generated 
   dynamically using the following custom field output –
 * product_image
    product_pdf product_thumb product_title
 * I’m really only guessing how to assign variables for those custom fields in the
   foreach loop and then reference them in the html. And each time I try – eg by
   amending the code to that below the browser (chrome) just returns a blank page–
   so, I know it’s wrong but I can’t find out why!
 *     ```
       <?php
       	$pages = get_pages('child_of='.$post>ID.'&sort_column=post_modified&sort_order=asc');
       	$count = 0;
       	foreach($pages as $page) :
       	$product_pdf = _get_field($page->ID, 'product_pdf');
       	{
       	?>
       <div class="thumbnail">
       <a href="<?php echo get_page_link($page->ID) ?>"><img src="<?php $upload_dir = wp_upload_dir(); ?><?php echo $upload_dir['baseurl']; ?>/B04002060-65-140x14	0.jpg" width="140" height="140"></a><br /><a href="<?php $upload_dir = wp_upload_dir(); ?><?php echo $upload_dir['baseurl']; ?>/<?=$product_pdf?>" target="_blank" title="Mini Balun 1.6/5.6 Male Screw  to suit 0.5-0.65mm STP/UTP 75/120 Ohm 2/8/34 Mbit/s"><img src="<?php bloginfo('template_directory'); ?>/images/pdf-icon-small.gif" width="16" height="16" alt="Link to PDF document" /></a><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a>
   
       </div>
       	<?php
       	}
       ?>
       ```
   
 * FInally, I want the number of boxes across the page to display relative to the
   page width – will this happen automatically or will I have to write into the 
   code where there needs to be a return to the next line??
 * If you have any advice/smarter code, I’m all ears and really appreciate the community’s
   help.
 * Thanks

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

 *  Thread Starter [deeleea](https://wordpress.org/support/users/deeleea/)
 * (@deeleea)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/advice-on-code-foreach/#post-1770771)
 * Hi, I solved the problem – it was the `_get_field` function – I hadn’t put the
   function in functions.php – once that was added everything worked!
 *  Thread Starter [deeleea](https://wordpress.org/support/users/deeleea/)
 * (@deeleea)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/advice-on-code-foreach/#post-1770772)
 *     ```
       function _get_field( $post_id, $field_key ) {
       	$fields = get_post_meta($post_id, $field_key);
       	if( count($fields) == 0)
       		return '';
       	if( count($fields) == 1)
       		return $fields[0];
   
       	return $fields;
       }
       ```
   
 * Oh, here’s the function code if anyone needs it.

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

The topic ‘Advice on Code foreach’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [deeleea](https://wordpress.org/support/users/deeleea/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/advice-on-code-foreach/#post-1770772)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
