0

I have some trouble making work a conditional inside a Function.

check this out:

I'm using this function:

function get_spec_1(){
  $desc1 = get_post_meta( get_the_ID(), 'eco_mb_tp_1', true!= ""  );

  if(!empty ($desc1)){
    foreach ( $desc1 as $d1 ){
      $code = "
      <dl>
      <dt class='bullet6-top'>General</dt>
      <dd>
      <ul>
      <li>$d1</li>
      </ul>
      </dd>
      </dl>
      ";
      echo $code;
    }

  }
}

But this function causes this:

<dl>
    <dt class='bullet6-top'>Generales</dt>
    <dd>
    <ul>
    <li>$metabox_cloned_value_1</li>
    </ul>
    </dd>
</dl>

<dl>
    <dt class='bullet6-top'>Generales</dt>
    <dd>
    <ul>
    <li>$metabox_cloned_value_2</li>
    </ul>
    </dd>
</dl>

But what I'm trying to achieve is this:

<dl>
    <dt class='bullet6-top'>Generales</dt>
    <dd>
    <ul>
<li>$metabox_cloned_value_1</li>
<li>$metabox_cloned_value_2</li>
    </ul>
    </dd>
</dl>

And if there is no existing data.

then the entire <dl> will not show.

How can I obtain this result?

thanks

6
  • You call get_post_meta() in a very strange way. What result do you expect? An array of all meta data with this key or just the last value?
    – fuxia
    Commented Oct 3, 2012 at 14:56
  • yeah, it is a clone metabox....
    – Locke
    Commented Oct 3, 2012 at 15:11
  • That doesn't answer my question?
    – fuxia
    Commented Oct 3, 2012 at 15:16
  • sorry, deluxeblogtips.com/meta-box/get-meta-values in the section : Get cloned values
    – Locke
    Commented Oct 3, 2012 at 15:24
  • Again (last attempt): Why do you set the last parameter of get_post_meta() not to a simple true or false? What is the expected result?
    – fuxia
    Commented Oct 3, 2012 at 15:31

1 Answer 1

3

Here you go. Because the DL, DT, DD, and UL are all within the foreach loop, that is why they are being duplicated. Here is the proper code:

function get_spec_1(){
  $desc1 = get_post_meta( get_the_ID(), 'eco_mb_tp_1', true );

  if( !empty( $desc1 ) ){
    // Open the stuff
    $code = "
      <dl>
      <dt class='bullet6-top'>General</dt>
      <dd>
      <ul>";
    // Loop through the items once inside the ul
    foreach ( $desc1 as $d1 ){
      code .="<li>$d1</li>";
    }
    // Close the stuff
    code.="
      </ul>
      </dd>
      </dl>
      ";
    echo $code;
  }
}
6
  • thanks for the fast answer... but , it does do the trick, put the <li> in the correct place, but the <dl> still there if the content doesn't exist: live url: ecolife.mx/productos/solatube/solar-star
    – Locke
    Commented Oct 3, 2012 at 15:08
  • It seems that because it is a cloned content , it will not recognise the $desc1 function...
    – Locke
    Commented Oct 3, 2012 at 15:17
  • What is this: true!= ""?
    – kaiser
    Commented Oct 3, 2012 at 15:39
  • toscho kaiser , was a terrible approach to get_post_meta(), nothing to achieve there... just trying non sense.. hahaha :)
    – Locke
    Commented Oct 3, 2012 at 15:42
  • I believe the issue is most likely the if statement. I am fixing it - before you had a space between !empty and it's brackets. Commented Oct 3, 2012 at 16:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.