将元数据作为“平面”数据添加到帖子中实际的问题是,在调用get_post_custom( $id );
为您的帖子。这里有一个简单的函数,它将附加到post的所有元数据添加到post对象。
/**
* 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;
}
然后,我们简单地用
<table>
. 您可以添加
<thead><th>etc.
之前和
<tfoot>etc.
之后那就把你所有的
<tr><td>Cell</td>etc.
在回路内部。
// 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>";