Support » Fixing WordPress » if-condition custom fields

  • Hi,

    i can’t really cope with php, that’s why i am looking for help regarding the following scenario:
    i would like to display the content of 2 custom fields inside a div, but only if there is any content to display. otherwise the div should not appear.

    my question sounds similiar to this one but i wasn’t able to solve my problem with the code snippets there.

    so far i used the following improvisational code on top of my sidebar:

    <?php if($key="Bonusbild-Class") { ?><br />
    <div class="<?php $key="Bonusbild-Class"; echo get_post_meta($post->ID, $key, true); ?>" align="center">
    <img src="<?php $key="Bonusbild"; echo get_post_meta($post->ID, $key, true); ?>"><br />
    <small4><?php $key="Bildunterschrift"; echo get_post_meta($post->ID, $key, true); ?></small4><br /></div><br /><br />
    <?php } ?>

    i have 3 conditional fields:
    “Bonusbild-Class” is for the surrounding div, but that one is actually always the same. it only has been important to me because i tried to build an if-condition with it at the beginning of the code.
    “Bonusbild” would be the picture and “Bildunterschrift” the text.

    this code works well if there is content in “Bonusbild” and / or “Bildunterschrift”. but if no custom fields are filled with information the following part of the sidebar starts beneath a big gab where the div-box would normally be. it seems that the “br”-tags are still working and that my attempt for an “if-condition” didn’t work entirely.

    does someone know how i could rewrite the code to let the sidebar begin on top even if there are no conditional fields to display ?

    i would be very glad for an answer.
    sorry for my english.

    bs.

Viewing 2 replies - 1 through 2 (of 2 total)
  • the problem is that you’re trying to do something relatively complex without any php knowledge at all…

    in order to check if something exists, it must first be defined, if it’s not defined, then you can’t accurately check if it’s there.

    Also, you should spend a little time reading about conditionals on the PHP site – it will help you greatly in future. For starters, “=” sets a value to a variable, whereas “==” is a comparative statement that translates to “is equal to” … etc…

    anyway, as for your code… try this:

    <?php
      unset($cfclass);
      unset($cfimg);
      unset($cfcaption);
      $cfclass = get_post_meta($post->ID, 'Bonusbild-Class', true);
      $cfimg = get_post_meta($post->ID, 'Bonusbild', true);
      $cfcaption = get_post_meta($post->ID, 'Bildunterschrift', true);  
    
      if ($cfclass && $cfimg && $cfcaption) :
    ?>
    
    <div class="<?php echo $cfclass; ?>" align="center">
      <img src="<?php echo $cfimg; ?>" /><br />
      <small4><?php echo $cfcaption; ?></small4><br />
    </div>
    <br />
    <br />
    
    <?php endif; ?>

    that extracts the contents to see if there’s anything in the custom fields BEFORE you try to actually echo/output it.

    I don’t use any custom fields so I havent tested this, so don’t sue me if it doesn’t work immediately 😉

    Thread Starter buntstift

    (@buntstift)

    wow, thank you very much. this code brings in some perfection. 🙂
    it seems to work 100% as it should, it’s exactly what i wanted.

    thank you for your patience and help !

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘if-condition custom fields’ is closed to new replies.