Tagged: get_post_meta, piklist::pre
- This topic has 7 replies, 2 voices, and was last updated 6 years, 4 months ago by
Steve.
-
AuthorPosts
-
-
September 13, 2015 at 5:33 pm #4317
cosmocanuckMemberHi guys!
I’m trying to prevent displaying a field if it’s empty – but for one repeating field, my standard approach is not working:
<?php $ticket_links = get_post_meta($post->ID, 'ticket_links', true); if (!empty($ticket_links[0]) ) { piklist('buy-tickets-template', array('data' => $ticket_links, 'loop' => 'data')); } ?>If I leave the “if” statement out, it runs fine, but then, even if I’ve deleted all repeating fields but the first one, and deleted all text in that first repeating field… I get something displayed (my template includes some code to make a button out of elements in the repeating field, and I get a sort of half-button even if my sole repeating field is empty.)
This is only if I’ve used the repeating field(s) but then want to “empty them out”. Is there a proper way to do that? Or to check first with a better “if” statement before displaying?
Thanks!
adam -
September 13, 2015 at 10:10 pm #4318
SteveKeymaster@cosmocanuck– Can you post your field code for
ticket_links? -
September 13, 2015 at 10:53 pm #4319
cosmocanuckMemberHere you go!
piklist('field', array( 'type' => 'group' ,'field' => 'ticket_links' ,'label' => __('Ticket Links') ,'columns' => 12 ,'add_more' => true ,'fields' => array( array( 'type' => 'datepicker' ,'field' => 'screeningdate' ,'label' => 'Screening Date' ,'columns' => 3 ,'options' => array( 'dateFormat' => 'M d, yy' ) ) ,array( 'type' => 'text' ,'field' => 'screeningURL' ,'label' => 'Full ticket URL' ,'columns' => 9 ) ) )); -
September 15, 2015 at 11:52 am #4325
SteveKeymaster@cosmocanuck— What version of Piklist are you using?
-
September 15, 2015 at 12:01 pm #4330
cosmocanuckMember0.9.4.28.
-
September 15, 2015 at 12:17 pm #4333
SteveKeymasterget_post_metahas a third parameter, which you set totrue. From the codex page for get_post_meta:
Whether to return a single value.However, in your code you are looking for an array key, not a single value:
$ticket_links[0], so you should set that parameter tofalse:
$ticket_links = get_post_meta($post->ID, 'ticket_links', false);The best way to debug this in the future is to look at your data. PHP has a function,
print_r, that helps you do this. However,print_rdoesn’t always display values in the easiest to read format, so Piklist has a function to help:piklist::pre(). You can use either.So, to debug in the future do something like this:
$ticket_links = get_post_meta($post->ID, 'ticket_links', true); piklist::pre($ticket_links);
You will see that there is no [0] key for you to check.
Now, change the third parameter to false:
$ticket_links = get_post_meta($post->ID, 'ticket_links', false); piklist::pre($ticket_links);
WordPress now added that key for you.
Hope that helps, and makes sense.
-
September 15, 2015 at 12:27 pm #4335
cosmocanuckMemberThanks Steve, that looks like exactly what I need – now to work through it… slowly! 8^)
-
September 15, 2015 at 12:33 pm #4337
SteveKeymasterGreat! Closing ticket.
-
-
AuthorPosts
- The topic ‘Checking for value of a repeating field seems not to work’ is closed to new replies.