Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

closed as off topic by kaiser, Michael, toscho Oct 4 '12 at 0:06

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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? –  toscho Oct 3 '12 at 14:56
    
yeah, it is a clone metabox.... –  Locke Oct 3 '12 at 15:11
    
That doesn't answer my question? –  toscho Oct 3 '12 at 15:16
    
sorry, deluxeblogtips.com/meta-box/get-meta-values in the section : Get cloned values –  Locke Oct 3 '12 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? –  toscho Oct 3 '12 at 15:31
show 1 more comment

1 Answer 1

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;
  }
}
share|improve this answer
    
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 Oct 3 '12 at 15:08
    
It seems that because it is a cloned content , it will not recognise the $desc1 function... –  Locke Oct 3 '12 at 15:17
    
What is this: true!= ""? –  kaiser Oct 3 '12 at 15:39
    
toscho kaiser , was a terrible approach to get_post_meta(), nothing to achieve there... just trying non sense.. hahaha :) –  Locke Oct 3 '12 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. –  Eric Holmes Oct 3 '12 at 16:57
show 1 more comment

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