• Resolved akadis2

    (@akadis2)


    My template is working almost perfect now… there is only one issue when trying to display the values of the multi-select fields. I tried following the instructions in the wiki, but have not been able to figure out how to display the values without the brackets.

    This is my code:

    <?php
    $Q = new GetPostsQuery();
    $args = array();
    $args['post_type'] = 'resource';
    $args['location']['like'] = '"Mississippi"'; // <-- note the quotes here
    
    $results = $Q->get_posts($args);
    foreach ($results as $r):
    ?>
    <table border="0" cellspacing="1" cellpadding="1" align="center">
    <tbody>
    <tr>
    <td align="center" valign="top"><?php print wp_get_attachment_image($r['thumb'], thumbnail); ?></td>
    <td valign="top"><h2> < a >rel="bookmark"> <?php print $r['post_title']; ?> < /a >
    </h2>
    <b>Type:</b> <?php print $r['type']; ?>  -   <b>Grade Level:</b> <?php print $r['grade_level']; ?>
    <?php print $r['post_excerpt']; ?></td>
    </tr>
    </tbody>
    </table>
    
    <?php
    endforeach;
    ?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    You can view the results here:

    http://civilrightsteaching.org/mississippir/

    As you can see, the Grade Level displays with the brackets. How can I remove them?

    Thanks again!

    http://ww.wp.xz.cn/plugins/custom-content-type-manager/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Multi-select or “repeatable” fields store their data as JSON arrays. When retrieving single posts, the get_custom_field() function helps translate this into something you can print (e.g. a formatted list), but when iterating over many posts, you have to convert things explicitly.

    So if your “location” field is storing an array of values, you need to iterate over each item in the array.

    // ... some filter criteria in $args (see above)
    $results = $Q->get_posts($args);
    foreach ($results as $r):
        // The manual way, using json_decode:
        $my_location_array = json_decode($r['location'],true);
        foreach ($my_location_array as $loc) {
            print $loc .'<br/>';
        }
    
        // Using a CCTM filter to help you out:
        print CCTM::filter($r['location'], 'formatted_list');
    
       // Or on a different field:
        print CCTM::filter($r['grade_level'], 'formatted_list');
    endforeach;

    See https://code.google.com/p/wordpress-custom-content-type-manager/wiki/formatted_list_OutputFilter

    The same must be done for any field that is storing multiple values (e.g. “grade_level” in your example). If you’re storing multiple values when you meant to store a single one, you’d have to clean up your data a bit (e.g. correcting your field definitions and re-saving posts that had incorrect data in them).

    Hope that helps.

    Thread Starter akadis2

    (@akadis2)

    It worked!! You’re awesome!!! Thanks for your help.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Issue with Brackets once again’ is closed to new replies.