Skip to content

Instantly share code, notes, and snippets.

@mch0lic
Created April 29, 2015 20:25
Show Gist options
  • Save mch0lic/4cb82d7fcdc47c19c7c4 to your computer and use it in GitHub Desktop.
Save mch0lic/4cb82d7fcdc47c19c7c4 to your computer and use it in GitHub Desktop.
<?php
/**
* Category and product tag og:image fix for SEO by yoast / woocommerce
*/
function fabframe_print_custom_og_image()
{
if ( is_product_category() ) {
global $post;
$args = array( 'taxonomy' => 'product_cat',);
$terms = wp_get_post_terms( $post->ID, 'product_cat', $args );
if ( count( $terms ) > 0 ) {
$og_image = get_field( 'custom_og_image', 'product_cat_' . $terms[0]->term_id );
if ( $og_image ) {
printf( '<meta property="og:image" content="%s" />', $og_image );
}
}
}
if ( is_product_tag() ) {
global $post;
$args = array( 'taxonomy' => 'product_tag',);
$terms = wp_get_post_terms( $post->ID, 'product_tag', $args );
if ( count( $terms ) > 0 ) {
$og_image = get_field( 'custom_og_image', 'product_tag_' . $terms[0]->term_id );
if ( $og_image ) {
printf( '<meta property="og:image" content="%s" />', $og_image );
}
}
}
}
add_action( 'wp_head', 'fabframe_print_custom_og_image' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment