在wordpress中显示所有帖子元键和相同帖子ID的元值
问题描述:
我正在尝试显示发布元值和发布元键,如果只显示一个值,我可以使用简单的函数get_post_meta(),但是我现在需要的是发布所有具有相同post_id的发布元数据.我尝试使用foreach循环,但没有任何显示.你能检查一下我的密码吗?
I'm trying to display post meta values and post meta keys, If only one value is to be display I can used the simple function get_post_meta() but what I need now is to post all post meta data with the same post_id. I tried using foreach loop but nothing displays. can you please check my codes?
function wpt_calendar_display()
{
global $post;
$columns = array(
'date_event' => 'Date',
'name_event' => 'Event'
);
register_column_headers('list-header_events', $columns);
$event_name = get_post_meta( $post->ID, '_event_name' );
// $event_date = get_post_meta( $post->ID, '_event_date', false );
$return .= "<table class=\"widefat\">";
$return .= "<tr>";
$return .= print_column_headers('list-header_events');
$return .= "</tr>";
$return .= "<tr>";
if (!empty($event_name))
foreach($event_name as $e_name)
{
$return .= "<td>";
$return .= $e_name;
$return .="</td>";
}
$return .= "<td>";
$return .= "</td>";
$return .= "</tr>";
$return .= "</table>";
return $return;
}
答
默认用法
获取所有键的元数据:
<?php $meta = get_post_meta($post_id); ?>
获取单个键的元数据:
Get the meta for a single key:
<?php $key_1_values = get_post_meta( 76, 'key_1' ); ?>
例如:
$myvals = get_post_meta($post_id);
foreach($myvals as $key=>$val)
{
echo $key . ' : ' . $val[0] . '<br/>';
}
注意:还会出现一些以下划线(_)"开头的不需要的元密钥,因此您需要将其过滤掉.
供参考:请参阅法典