- This topic has 12 replies, 2 voices, and was last updated 5 years, 9 months ago by
Jason.
-
AuthorPosts
-
-
April 15, 2016 at 6:58 pm #6297
4michaelcolemanMemberSo trying to do a simple if / else statement that if my data is empty display nothing (in my example I’m putting in the p tag). And for some reason it will not work when I empty my data and update the page.
Here is what the basic code looks like…
<?php $silver = get_post_meta($post->ID, 'summit_sponsor_silver_group', true); ?> <?php if (!empty ($silver)) : ?> // Standard HTML code here <?php else : ?> <p>This is empty</p> <?php endif; ?> -
April 18, 2016 at 11:45 pm #6302
JasonKeymasterHi @4michaelcoleman!
What’s the value of
$silver? Have you tried something likeprint_r($silver)to see its value? -
April 19, 2016 at 1:19 pm #6316
4michaelcolemanMember@Jason here are examples of what it looks like on the page for both instances (attached). As you can see the html is still present even when there is no content. Im not getting the version where I am simply writing the “p” tag with this is empty. So not sure why this is not taking the “else” call.
Attachments:
You must be logged in to view attached files. -
April 19, 2016 at 1:32 pm #6318
JasonKeymasterIt’s hard to say. If I had to guess I’d say the value of
$silverprobably isn’t what your expecting, which is why I suggest doing something like:<?php $silver = get_post_meta($post->ID, 'summit_sponsor_silver_group', true); print_r($silver); -
April 19, 2016 at 1:52 pm #6319
4michaelcolemanMember@Jason Here is the output of print
Array ( [0] => Array ( [sponsor_silver_name] => TSI Co [sponsor_silver_overview] => Sample text for this sponsor [sponsor_silver_url] => http://www.logo.com [upload_basic] => Array ( [0] => 10412 ) ) )
-
April 19, 2016 at 1:52 pm #6320
4michaelcolemanMemberArray ( [0] => Array ( [sponsor_silver_name] => TSI Co [sponsor_silver_overview] => Sample text for this sponsor [sponsor_silver_url] => http://www.logo.com [upload_basic] => Array ( [0] => 10412 ) ) )
-
April 19, 2016 at 1:57 pm #6321
JasonKeymasterHey @4michaelcoleman!
Gotcha. I had a feeling that was the structure. So the reason that
empty($silver)doesn’t work is because even if the fields are empty, it’s still an array with those indexes. So you may want to consider which field is required minimally. So if it’s sponsor_silver_name, then useempty($silver['sponsor_silver_name']).Make sense?
-
April 19, 2016 at 2:01 pm #6322
4michaelcolemanMember@Jason hmmm… the issue is I don’t want anything to show if there isn’t a sponsor. Meaning I am trying to hide the whole segment if there isn’t a sponsor for this level. So it seems that if sponsor_silver_name is empty then do nothing, correct?
-
April 19, 2016 at 2:17 pm #6323
JasonKeymasterCorrect. When you have a group in Piklist there’s always going to be an array with the fields saved. So even thought we think of it as empty, it’s still saving the fields as an array, albeit with empty values (or whatever the default value for the individual field is). So I’m assuming that to signify that there is no sponsor you’re simply leaving the group empty, in which case you have to determine which field in the group is how you’d determine that and check whether that field is empty.
Another option I’ve used is to have a radio field that has a title such as “Has a sponsor”, and the values “Yes” and “No”. Then conditionally show/hide the group based on that radio. Then you would check against the radio button to determine if there is or isn’t a sponsor.
-
April 19, 2016 at 2:44 pm #6324
4michaelcolemanMember@jason so here is what I am doing and still get the “is empty” instead of the content. Even when there is content submitted in my post for the silver level.
<?php $silver = get_post_meta($post->ID, 'summit_sponsor_silver_group', true); ?> <?php if (empty($silver['sponsor_silver_name'])) : ?> <p>is empty</p> <?php else : ?> // FULL HTML Code <?php endif; ?> -
April 19, 2016 at 6:07 pm #6325
JasonKeymasterCan you please paste the field code? Is this an add-more field?
-
April 19, 2016 at 8:08 pm #6326
4michaelcolemanMember@jason – yes it is.
piklist('field', array( 'type' => 'group', 'field' => 'summit_sponsor_silver_group', 'add_more' => true, 'description' => 'Add sponsors as needed', 'fields' => array( array( 'type' => 'text', 'field' => 'sponsor_silver_name', 'label' => 'Sponsor Name', 'required' => false, 'columns' => 12 ), array( 'type' => 'textarea', 'field' => 'sponsor_silver_overview', 'label' => 'Sponsor Overview', 'required' => false, 'attributes' => array( 'rows' => 8, 'class' => 'large-text', ), 'columns' => 12 ), array( 'type' => 'text', 'field' => 'sponsor_silver_url', 'label' => 'Sponsor URL', 'required' => false, 'columns' => 12 ), array( 'type' => 'file', 'field' => 'upload_basic', 'label' => __('Sponsor Logo', 'piklist'), 'description' => __('Image Size: 00px X 00px','piklist'), 'required' => false, 'options' => array( 'modal_title' => __('Add Logo', 'piklist'), 'button' => __('Add Logo', 'piklist'), ), 'columns' => 4 ), ), 'on_post_status' => array( 'value' => 'lock' ) )); -
April 19, 2016 at 8:35 pm #6327
JasonKeymasterAh, gotcha. We ran into this in the other thread, too. When you grab
$silveryou’re getting every instance of the group. So you have an array of arrays with the fields as indexes. So the first name would be$silver[0]['sponsor_silver_name']. You’ll have to be creative to determine if there are any silver groups as they may have an empty first. So something like the following:function is_empty_group($group) { foreach($group as $item) { if ( $item['sponsor_silver_name'] ) { return false; } } return true; } // Then used as.. $silver = get_post_meta($post->ID, 'summit_sponsor_silver_group', true); if ( is_empty_group($silver) ) { } else { }I strongly recommend taking the concept of what I’m doing here and forming your own solution rather than taking it directly. I’m just throwing this together conceptually. 🙂
-
-
AuthorPosts
- You must be logged in to reply to this topic.