I’m attempting to use ACF for the featured image and open graph image for a custom taxonomy I’ve set up. Adding and applying the custom field to the taxonomy was easy. But trying to create something to go in my functions has been a bit more challenging for me.
The custom taxonomy is “brands”, and it’s used as an identifier on regular ol’ pages. No custom post type.
This is what I have so far…
add_action('wp_head', 'rjrasmussen_brand', 5);
function rjrasmussen_brand( ) {
// If it's not a taxonomy, die.
if ( !is_tax('brands') ) {
return;
}
if (get_field('featured_image', $post->ID)) {
$image = base_image_url(get_field('featured_image', $page->ID), null);
}
echo '<meta property="og:image" content="'.$image.'" />';
}
The echo works fine, but nothing is getting passed to the content.
Any thoughts or suggestions to get me going? Maybe there’s a far better solution out there that I just couldn’t locate?
Huge pre-emptive thank you! 🙂
$page->ID
is never set and $post->ID
has no value inside of your function.
add_action('wp_head', 'rjrasmussen_brand', 5);
function rjrasmussen_brand( ) {
// If it's not a taxonomy, die.
if ( !is_tax('brands') ) {
return;
}
global $post;
if (get_field('featured_image', $post->ID)) {
$image = base_image_url(get_field('featured_image', $post->ID), null);
}
echo '<meta property="og:image" content="'.$image.'" />';
}