farinspace / wpalchemy

Thin framework for wordpress
http://wpalchemy.com/
Other
416 stars 110 forks source link

REPEATABLE FIELDS: Checking if fields exist #75

Closed cibulka closed 11 years ago

cibulka commented 11 years ago

Hello everyone,

first of all, I want to point out that i see WPAlchemy as the ultimate solution for most of the post meta situation you can run into on building the Worpdress website! I especially love the architecture that allows very advanced customization.

Back to my issue: One thing I can't crack is how to ask if repeatable fields exist without breaking the index. If I have 4 fields in the group, this code -

if ($mb_general->have_fields('meta') {
    while($mb_general->have_fields('meta)) {
        $meta = $mb_general->get_the_value('meta');
        echo $meta;
    }
}

Is there any safe way to check if the fields exist? On Farin's website, I can't seem to find anything of the sort.

Thank you! Petr

cibulka commented 11 years ago

Okay, as pointed out by zach - http://www.farinspace.com/forums/topic/have_fields-conditional-weirdness/ - there is some bizarre behaviour of have_fields function under conditionals.

If you use $meta method, everything works just fine. Here's my updated code:

$meta = $mb_general->the_meta();
if ($meta['friends']) {
    echo'<section id="friends"><h4>Friends</h4><ul>';
    foreach($meta['friends'] as $friend) {
        $url = $friend['friends_url'];
        echo'<li>'.$url.'</li>';
    }
        echo'</ul></section>';
}

This works!