You are probably using this code in your theme’s header.php
. When this file is loaded $post
is not available yet (it’ll be inside the Loop). So you’d need to declare before anything global $post;
.
But that’s not the best approach. If you swap themes, what? Do it again?
See: Where to put my code: plugin or functions.php?
With the following plugin, we will hook into wp_head
and print our meta based on some Conditional Tags:
<?php
/* Plugin Name: Print Audio OG */
add_action( 'wp_head', 'print_audio_og_wpse_99152', 0 );
function print_audio_og_wpse_99152()
{
// Print only in single posts or pages
if( is_single() || is_page() )
{
global $post;
$meta_value = get_post_meta( $post->ID, 'meta_audio_attachment', true );
if( $meta_value )
echo "
<meta property='og:audio' content="$meta_value" />
<meta property='og:audio:title' content="$post->post_title" />
<meta property='og:audio:artist' content="Test" />
<meta property='og:audio:album' content="Test Album" />
<meta property='og:audio:type' content="application/mp3" />
";
}
}
You’ll have to adapt get_post_meta
.
Increase the hook priority, now 0
(zero), to move the insertion point in the rendered HTML.