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 created a meta box and it's storing the post data successfully but I am having trouble outputting the content and displaying it in a sensible fashion - beyond my limited knowledge of PHP. I am a bee-keeper and want to record inspections of my beehives - so for each hive there is a record with a set number of fields.

So I want to loop through the colony array for all hives, i.e. until $hive is empty, and output the data in the form of a table with 1 row per hive. Currently I have managed to manually produce the first row of the table but don't know how to set-up a loop that could do it for me.

<?php if (have_posts() ):?>

        <?php while ( have_posts() ) :the_post();?>
    <?php
    $meta=$simple_mb->the_meta();
     // need a loop here until all instances colony are reached 
        $hive = $meta['colony'] [0]['hive']; 
                $queen = $meta['colony'] [0]['cb_ex4_cb'];
        $bias = $meta['colony'] [0]['cb_ex4_bias'];
        $brood = $meta['colony'] [0]['cb_ex4_bcom'];
        $stores = $meta['colony'] [0]['stores'];
        $mycomm = $meta['colony'] [0]['comment'];
        $plan = $meta['colony'] [0]['plan'];
    ?>

            <table id="anytable" summary="Colony Inspections" cellspacing="0">
            <caption>        < ?php echo   $inspect;?> 
            <?php       the_title(); ?></caption>           
            <tbody>

    <tr>        <!-- want additional rows added for each colony -->
    <th scope="row"><?php echo $hive; ?></th> 
    <td><div><?php echo $queen; ?></div></td>
    <td><div><?php echo $brood; ?></div></td>
    <td><div><?php echo $stores; ?></div></td>
    <td><?php echo $mycomm; ?></td>
    <td><?php echo $plan; ?></td>
    </tr>
    </tbody>
    </table>


    <?php endwhile; ?>

    <?php endif; ?>
share|improve this question
    
What's $simple_mb->the_meta()? It's a custom or plugin function, but you need to tell from where it is and how it works. Btw: You'll have to replace get_post_custom() with your fn in my example. But for later readers I leave the answer with a native WP function. –  kaiser Feb 15 '12 at 20:34

2 Answers 2

Add meta data as "flat" data to the post

The actual problem is, that your meta data comes back as array (with a single entry), when calling get_post_custom( $id ); for your post. Here's a simple function, that adds all meta data attached to a post to your post object.

/**
 * Merges custom/meta data with post data
 * 1) get_post_custom() is cached from the WP_Query
 * 2) The following line gets the first element from each meta array as it's only a single value
 * It then merges the resulting flattened Array with the post object that gets casted to another Array
 * 
 * @param array $query
 * @return array result 
 */
public function add_meta_data( $query )
{
    foreach ( $query as $post )
        $result[] = array_merge( 
             array_map( 'array_shift', get_post_custom( $post->ID ) )
            ,(array) $post 
        );

    return $result;
}

How-to use

We then run the loop simply wrapped in a <table>. You can add your <thead><th>etc. before and the <tfoot>etc. afterwards. Then just echo all your <tr><td>Cell</td>etc. inside the loop.

// Before your loop:
global $wp_query;
// Merge the queried posts with their meta data, using our custom template tag/function
$query = add_meta_data( $wp_query->posts );

echo "<table>";
// Loop through our new query array
foreach ( $query as $post )
{
        // This extracts your post object, so you don't have to save them in separate vars
        // @see @link http://php.net/manual/de/function.extract.php
        extract( $post, EXTR_SKIP );

        echo "<tr>";
        // Then simply call all your meta data like this:
        echo "<td>{$hive}</td>";
        // Then add other meta data here:
        echo "<td>{$whatever}</td>";
        echo "</tr>";
}
echo "</table>";
share|improve this answer
    
Seems to fail on the while ($query->have_posts() ) - gives an error message (shortened) - CALL TO A MEMBER FUNCTION HAVE_POSTS() ON A NON-OBJECT. A print_r of $query shows that it is holding all the post data. Andy ideas on how to overcome the error? –  andy rob Feb 17 '12 at 11:38
    
Sorry meant to say that the error is on "if ($query->have_posts() ){" - gives an error message (shortened) - CALL TO A MEMBER FUNCTION HAVE_POSTS() ON A NON-OBJECT. A print_r of $query shows that it is holding all the post data. Andy ideas on how to overcome the error? –  andy rob Feb 17 '12 at 11:47
    
Ok, you're better off with a foreach loop. –  kaiser Feb 17 '12 at 12:24
    
(see edit of the answer for a foreach example) –  kaiser Feb 19 '12 at 14:52

You could try something like this...

// need a loop here until all instances colony are reached 

foreach( $meta['colony'] as $colony ){
    echo '<tr>';
    foreach( $colony as $key => $value ){
        //$key may contain 'cb_ex4_cb'
        //$value may contain what you put in $queen
        echo '<td>$value</td>'; 
    }
    echo '</tr>';
}
share|improve this answer
    
Thanks - the foreach code is working and outputting the results. Now I just need to sort out the formatting of the data into a table, which is bit more involved and will take a bit of time. –  andy rob Feb 15 '12 at 18:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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