Last active
July 19, 2023 23:44
-
-
Save BruceMcKinnon/8dd75ca16821be1ce2fa2f72451b4af4 to your computer and use it in GitHub Desktop.
Create a custom Yoast SEO Variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Add a Custom Yoast Variable | |
// | |
add_action('wpseo_register_extra_replacements', 'add_yoast_supercharge_variables'); | |
function add_yoast_supercharge_variables() { | |
if (function_exists ('wpseo_register_var_replacement')){ | |
wpseo_register_var_replacement('%%BRAND%%', 'supercharge_yoast_brand', 'advanced', 'Supercharge Brand' ); | |
} | |
} | |
function supercharge_yoast_brand() { | |
$post_id = get_the_ID(); | |
$this_brand = ''; | |
if ( get_post_type($post_id) == 'battery' ) { | |
$brands = get_the_terms( $post_id, 'brands' ); | |
if ( $brands ) { | |
if (count($brands) > 0) { | |
$this_brand = $brands[0]->name; | |
} | |
} | |
} | |
return $this_brand; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment