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 (https://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_titleI’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
- The topic ‘Advice on Code foreach’ is closed to new replies.