December 8, 2014 at 4:15 pm #3003
Hi! I’ve been re-checking and re-comparing my code to the similar version that works, until my eyes cross, but can’t find any apparent error in my code; yet I get errors instead of data on my page.
My metafield code (just showing the first field since that’s the one I’m having trouble with, the other fields display fine):
<?php /* Title: Other Product Info Post Type: product Priority: high */ piklist('field', array( 'type' => 'text' ,'field' => 'claims' ,'columns' => 12 ,'add_more' => true ,'label' => __('Claims') ,'attributes' => array( 'class' => 'text' ) ));
And my single-product.php for displaying:
// Grab the claims $productClaims = get_post_meta($post->ID, 'claims', true); // Display the claims echo '<ul>'; piklist(get_stylesheet_directory() . '/product-claims-template', array('data' => $productClaims, 'loop' => 'data') ); echo '</ul>';
Finally, my product-claims-template.php:
<?php echo '<li>'; echo $data['claims']; echo '</li>'; ?>
And the errors it generates follows. The last one seems key. Surely the “claims” array is being correctly referenced, but…?
Warning: array_keys() expects parameter 1 to be array, string given in /home/soyqueen/public_html/superior2015/wp-content/plugins/piklist/includes/class-piklist.php on line 827
Warning: Invalid argument supplied for foreach() in /home/soyqueen/public_html/superior2015/wp-content/plugins/piklist/includes/class-piklist.php on line 256
Warning: Invalid argument supplied for foreach() in /home/soyqueen/public_html/superior2015/wp-content/plugins/piklist/includes/class-piklist.php on line 264
Notice: Undefined index: claims in /home/soyqueen/public_html/superior2015/wp-content/themes/genesis-sample/product-claims-template.php on line 4
Hope I’m not missing something obvious but it really seems like this should work… thanks!
(BTW, I’m on 9.4.23 of Piklist.)
December 9, 2014 at 12:21 pm #3010SteveKeymaster
@cosmocanuck– I found two issues:
1) In single-product.php,
get_post_metashould be set to
false, so it returns an array. I updated the documentation to reflect this (Under the “Display” area). Let me know if the documentation makes sense.
2) In product-claims-template.php, you already have the array data in
$data. There is no
$data['claims'], so just use
Let me know if that fixes it for you.
December 9, 2014 at 1:33 pm #3014
Beautiful! Thanks! I understand that it’s up to me to figure out why the get_post_meta would want to be true vs. false… but in merely comparing the User Guide example to my own attempt here, I can’t get my head around what the difference is, that mine would need to be “false” and the example is “true”, when they’re both referring to a single, add_more text field.
echo $data;in particular I just can’t figure out the subtlety of. In the example for the template file:
<?php echo $data['exercise']; ?>
The “$data” variable seems to be making its first appearance, and be dependent on naming the ‘exercise’ array even though it’s already been referenced in the first line of the theme code which is referring to the template:
$exercise = get_post_meta($post->ID, 'exercise', true);
Am I missing some obvious difference between the example situation and my own?
At any rate, thanks for the fix, I’m so happy it’s working now! Just want to be sure I understand why, so i don’t need to bug you again about this…. 8^)
December 9, 2014 at 2:54 pm #3015
December 10, 2014 at 9:34 am #3019JasonKeymaster
As far as the the true/false in the final parameter of the get_post_meta function goes, it’s really a simple concept: true if you want only a single row from the database; false if you want every row it finds that matches the meta name.
The only trick is knowing what’s stored as a single row and what’s not. So here it is:
- Single/Add_More Groups => Single row (true)
- Single non-group field => Single row (true)
- Add_More non-group field => Multiple rows (false)
Really not too bad! 🙂 The only unexpected one is that a group field with add_more is still a single row. The reason for this is that all the data is stored as a single, serialized array.
December 10, 2014 at 6:14 pm #3033
Thanks Jason! That’s awesome and will really help me get the hang of this.
- The topic ‘Error when attempting to display a single ungrouped add_more field’ is closed to new replies.