php 在wordpress中显示相同帖子ID的所有帖子元键和元值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22216276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display all post meta keys and meta values of the same post ID in wordpress
提问by user2901740
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?
我正在尝试显示后元值和后元键,如果只显示一个值,我可以使用简单的函数 get_post_meta() 但我现在需要的是使用相同的 post_id 发布所有后元数据。我尝试使用 foreach 循环,但没有显示任何内容。你能检查我的代码吗?
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;
}
回答by Gopal S Rathore
Default Usage
Get the meta for all keys:
默认用法
获取所有键的元数据:
<?php $meta = get_post_meta($post_id); ?>
Get the meta for a single key:
获取单个键的元数据:
<?php $key_1_values = get_post_meta( 76, 'key_1' ); ?>
for example:
例如:
$myvals = get_post_meta($post_id);
foreach($myvals as $key=>$val)
{
echo $key . ' : ' . $val[0] . '<br/>';
}
Note: some unwanted meta keys starting with "underscore(_)" will also come, so you will need to filter them out.
注意:一些以“下划线(_)”开头的不需要的元键也会出现,因此您需要将它们过滤掉。
For reference: See Codex
供参考:见食典
回答by alpc
$myvals = get_post_meta( get_the_ID());
foreach($myvals as $key=>$val){
foreach($val as $vals){
if ($key=='Youtube'){
echo $vals
}
}
}
Key = Youtube videos all meta keys for youtube videos and value
Key = Youtube 视频 youtube 视频的所有元键和值
回答by Ahmad Awais
I use it in form of a meta box. Here is a function that dumps values of all the meta data for post.
我以元框的形式使用它。这是一个转储所有元数据值的函数以供发布。
function dump_all_meta(){
echo "<h3>All Post Meta</h3>";
// Get all the data.
$getPostCustom=get_post_custom();
foreach( $getPostCustom as $name=>$value ) {
echo "<strong>".$name."</strong>"." => ";
foreach($getPostCustom as $name=>$value) {
echo "<strong>".$name."</strong>"." => ";
foreach($value as $nameAr=>$valueAr) {
echo "<br /> ";
echo $nameAr." => ";
echo var_dump($valueAr);
}
echo "<br /><br />";
}
} // Callback funtion ended.
Hope it helps. You can use it inside a meta box or at the front-end.
希望能帮助到你。您可以在元框内或前端使用它。
回答by Deepak Arora
To get all rows, don't specify the key. Try this:
要获取所有行,请不要指定键。尝试这个:
$meta_values = get_post_meta( get_the_ID() );
var_dump( $meta_values );
Hope it helps!
希望能帮助到你!
回答by Khayrattee Wasseem
As of Jan 2020 and WordPress v5.3.2, I confirm the following works fine.
截至 2020 年 1 月和 WordPress v5.3.2,我确认以下工作正常。
It will include the field keys with their equivalent underscore keys as well, but I guess if you properly "enum" your keys in your code, that should be no problem:
它还将包括带有等效下划线键的字段键,但我想如果您在代码中正确“枚举”了您的键,那应该没问题:
$meta_values = get_post_meta( get_the_ID() );
$example_field = meta_values['example_field_key'][0];
//OR if you do enum style
//(emulation of a class with a list of *const* as enum does not exist in PHP per se)
$example_field = meta_values[PostTypeEnum::FIELD_EXAMPLE_KEY][0];
As the print_r(meta_values);gives:
正如print_r(meta_values);给出的:
Array
(
[_edit_lock] => Array
(
[0] => 1579542560:1
)
[_edit_last] => Array
(
[0] => 1
)
[example_field] => Array
(
[0] => 13
)
)
Hope that helps someone, go make a ruckus!
希望能帮到大家,加油!
回答by John Henrique
WordPress have the function get_metadatathis get all meta of object (Post, term, user...)
WordPress 具有get_metadata函数,它可以获取对象的所有元数据(帖子、术语、用户...)
Just use
只需使用
get_metadata( 'post', 15 );

