0

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.com/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.

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'] : '';
  • This code breaks the template. Not working :( – Alex Dec 19 '18 at 18:17
  • missed a closing parens. fixed now – mrben522 Dec 19 '18 at 18:18
  • YES! The first code now works perfectly. Thank you very much for taking the time to help me! – Alex Dec 19 '18 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.