在数组中显示后Meta值

时间:2017-04-09 11:29:02

标签: 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(    
                '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;
}

例如,数据库中针对帖子的值为:

a:3:{i:0;s:10:"youngsters";i:1;s:5:"youth";i:2;s:6:"adults";}

我正在尝试在前端检索这些值,我无法使用此功能。我得到Nothing作为输出:

function get_event_age_group()
{
    global $post;

    $data = get_post_meta( $post->ID, '_event_age_group' );
    $age_groups = $data['options'];
    return $age_groups; 

}

任何人都可以帮助我无法获取数据吗?感谢

1 个答案:

答案 0 :(得分:0)

我能够通过以下功能获得输出。虽然我在编码方面没有那么多经验。我知道他们必须是一个更好的方法来做到这一点,但我能够以某种方式实现这一目标。这是对我有用的功能。这可能对某人有帮助。

/*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/>";
    }
}