$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Get a value inside a get_post_meta Array|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Get a value inside a get_post_meta Array

matteradmin10PV0评论

I am editing a Wordpress theme and want to output a value that's inside an array in the post meta array.

My code is:

$banner_subtitle = get_post_meta( $post_id, 'header_section_meta', true );

echo print_r($banner_subtitle);

And my output is:

Array
(
[section_mode] => banner
[header_section_id] => 
[slider_alias] => 
[banner_subtitle] => SUBTITLE 1
[banner_image] => .jpg
[is_banner_image_parallax] => 0
[banner_image_repeat] => repeat-x
[banner_mask] => 
)
1

How can I modify my code so that the output is the value of the banner_subtitle, in this case "SUBTITLE 1", instead of that whole array? Thank you.

I am editing a Wordpress theme and want to output a value that's inside an array in the post meta array.

My code is:

$banner_subtitle = get_post_meta( $post_id, 'header_section_meta', true );

echo print_r($banner_subtitle);

And my output is:

Array
(
[section_mode] => banner
[header_section_id] => 
[slider_alias] => 
[banner_subtitle] => SUBTITLE 1
[banner_image] => http://www.example/image.jpg
[is_banner_image_parallax] => 0
[banner_image_repeat] => repeat-x
[banner_mask] => 
)
1

How can I modify my code so that the output is the value of the banner_subtitle, in this case "SUBTITLE 1", instead of that whole array? Thank you.

Share Improve this question asked Dec 19, 2018 at 18:07 AlexAlex 1811 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
$banner_subtitle = get_post_meta( $post_id, 'header_section_meta', true );
if (!empty($banner_subtitle['banner_subtitle'])) {
    echo $banner_subtitle['banner_subtitle'];
}

or as a ternary

$banner_subtitle = get_post_meta( $post_id, 'header_section_meta', true );
echo !empty($banner_subtitle['banner_subtitle']) ? $banner_subtitle['banner_subtitle'] : '';

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far