0

Currently I have a select field with multiple string values in a custom node. I'm trying to print them out without any html markup.

Currently I am declaring my variable like this:

$categories = $content['field_category']; 

And I'm trying to print it like this:

foreach ($categories['und'] as $item) {
    echo $item['value'];
}

But nothing gets printed out. What am I doing wrong?

2
  • 1
    have you inspected $item with dpm() or similar stuff?
    – DimG
    Nov 5, 2013 at 23:59
  • is this in a menu_hook() ? if so i don't think it will work you need to provide the callback with renderable html. Nov 6, 2013 at 11:42

1 Answer 1

0

I ended up just making a custom loop like this and it works fine (Also added a custom if statement to change html output based on single or multiple categories in the field.

<?php 
    $categoryCount = count($field_category);
    if ($categoryCount != 1):
?>
    <p><strong>Categories:</strong>
<?php else: ?>
    <p><strong>Category:</strong>
<?php
    endif;
    $i = 0;
    while ($i < $categoryCount) {
        if ($i != 0) {
            $html .= ', ';
        }
        $html .= $field_category[$i]['value'];
        $i++;
    }
    echo $html;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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