php - Get a value inside a get_post_meta Array

admin2025-06-05  3

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'] : '';
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749083824a316221.html

最新回复(0)