Skip to content

Instantly share code, notes, and snippets.

@wp-seopress
Last active September 4, 2023 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wp-seopress/58272742bd6a1c4b9beb58ac580c1e75 to your computer and use it in GitHub Desktop.
Save wp-seopress/58272742bd6a1c4b9beb58ac580c1e75 to your computer and use it in GitHub Desktop.
Dynamic variable for SEOPress: get term parents list of the current term taxonomy
function sp_titles_template_variables_array($array) {
$array[] = '%%parent_terms_title%%';
return $array;
}
add_filter('seopress_titles_template_variables_array', 'sp_titles_template_variables_array');
function sp_titles_template_replace_array($array) {
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$tax = $queried_object->taxonomy;
$args = [
'format' => 'name',
'separator' => ', ',
'link' => false,
'inclusive' => false
];
$ancestors = get_term_parents_list($term_id, $tax, $args);
if ($ancestors && is_string($ancestors)) {
$array[] = esc_attr(wp_strip_all_tags($ancestors));
}
return $array;
}
add_filter('seopress_titles_template_replace_array', 'sp_titles_template_replace_array');
function sp_get_dynamic_variables($array){
$array['%%parent_terms_title%%'] = __('Parent terms title', 'my-text-domain');
return $array;
}
add_filter('seopress_get_dynamic_variables', 'sp_get_dynamic_variables');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment