Php 在数组中显示Post元数据
我有以下功能:Php 在数组中显示Post元数据,php,wordpress,post,meta,Php,Wordpress,Post,Meta,我有以下功能: function frontend_add_age_group_field ( $fields ) { $fields['event']['event_age_group'] = array( 'label' => __( 'Age Group', 'event_manager' ), 'type' => 'multiselect', 'options' => array(
function frontend_add_age_group_field ( $fields ) {
$fields['event']['event_age_group'] = array(
'label' => __( 'Age Group', 'event_manager' ),
'type' => 'multiselect',
'options' => array(
'kids' => __( 'Kids (7-10 Years)', 'wp-event-manager' ),
'youngsters' => __( 'Youngsters (10-14 Years)', 'wp-event-manager' ),
'youth' => __( 'Youth (15-25)', 'wp-event-manager' ),
'adults' => __( 'Adults (25+)', 'wp-event-manager' ),
),
'required' => true,
'priority' => 4,
);
return $fields;
}
例如,数据库中针对post的值为:
a:3:{i:0;s:10:"youngsters";i:1;s:5:"youth";i:2;s:6:"adults";}
我试图在前端检索这些值,但使用此函数无法获取这些值。我没有得到任何输出:
function get_event_age_group()
{
global $post;
$data = get_post_meta( $post->ID, '_event_age_group' );
$age_groups = $data['options'];
return $age_groups;
}
有人能帮我解释为什么我无法获取数据吗?谢谢我能用下面的函数得到输出。虽然我在编码方面没有那么多经验。我知道他们一定是做这件事的一个更好的方法,但我还是设法做到了。这是对我有用的函数。它可能会帮助某人
/*Function to get Age Groups from Events */
function get_event_age_group()
{
global $post;
$data = get_post_meta( $post->ID, '_event_age_group', false);
//$age_groups = $data['options'];
foreach ($data as $key=>$value) {
switch ($value[0]) {
case "kids":
echo "Kids (7-10 Years)";
break;
case "youngsters":
echo "Youngsters (10-14 Years)";
break;
case "youth":
echo "Youth (15-25)";
break;
case "adults":
echo "Adults (25+)";
break;
}
echo "<br/>";
switch ($value[1]) {
case "kids":
echo "Kids (7-10 Years)";
break;
case "youngsters":
echo "Youngsters (10-14 Years)";
break;
case "youth":
echo "Youth (15-25)";
break;
case "adults":
echo "Adults (25+)";
break;
}
echo "<br/>";
switch ($value[2]) {
case "kids":
echo "Kids (7-10 Years)";
break;
case "youngsters":
echo "Youngsters (10-14 Years)";
break;
case "youth":
echo "Youth (15-25)";
break;
case "adults":
echo "Adults (25+)";
break;
}
echo "<br/>";
switch ($value[3]) {
case "kids":
echo "Kids (7-10 Years)";
break;
case "youngsters":
echo "Youngsters (10-14 Years)";
break;
case "youth":
echo "Youth (15-25)";
break;
case "adults":
echo "Adults (25+)";
break;
}
echo "<br/>";
}
}
/*从事件中获取年龄组的函数*/
函数get\u event\u age\u group()
{
全球$员额;
$data=get_post_meta($post->ID,“_event_age_group”,false);
//$age_groups=$data['options'];
foreach($key=>$value形式的数据){
开关($value[0]){
“儿童”案:
echo“儿童(7-10岁)”;
打破
“青少年”个案:
回声“青少年(10-14岁)”;
打破
“青年”案:
回声“青年(15-25)”;
打破
案例“成年人”:
回声“成人(25+);
打破
}
回声“
”;
开关($value[1]){
“儿童”案:
echo“儿童(7-10岁)”;
打破
“青少年”个案:
回声“青少年(10-14岁)”;
打破
“青年”案:
回声“青年(15-25)”;
打破
案例“成年人”:
回声“成人(25+);
打破
}
回声“
”;
开关($value[2]){
“儿童”案:
echo“儿童(7-10岁)”;
打破
“青少年”个案:
回声“青少年(10-14岁)”;
打破
“青年”案:
回声“青年(15-25)”;
打破
案例“成年人”:
回声“成人(25+);
打破
}
回声“
”;
开关($value[3]){
“儿童”案:
echo“儿童(7-10岁)”;
打破
“青少年”个案:
回声“青少年(10-14岁)”;
打破
“青年”案:
回声“青年(15-25)”;
打破
案例“成年人”:
回声“成人(25+);
打破
}
回声“
”;
}
}