Viewing 12 reply threads
  • Author
    Posts
    • #6297

      So 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; ?>
    • #6302
      Jason
      Keymaster

      Hi @4michaelcoleman!

      What’s the value of $silver? Have you tried something like print_r($silver) to see its value?

    • #6316

      @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.
    • #6318
      Jason
      Keymaster

      It’s hard to say. If I had to guess I’d say the value of $silver probably 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);
    • #6319

      @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 ) ) )

    • #6320

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

    • #6321
      Jason
      Keymaster

      Hey @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 use empty($silver['sponsor_silver_name']).

      Make sense?

    • #6322

      @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?

    • #6323
      Jason
      Keymaster

      Correct. 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.

    • #6324

      @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; ?>
    • #6325
      Jason
      Keymaster

      Can you please paste the field code? Is this an add-more field?

    • #6326

      @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'
          )
        ));
    • #6327
      Jason
      Keymaster

      Ah, gotcha. We ran into this in the other thread, too. When you grab $silver you’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. 🙂

Viewing 12 reply threads
  • You must be logged in to reply to this topic.