• Resolved deeleea

    (@deeleea)


    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_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

    (@deeleea)

    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

    (@deeleea)

    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.