Custom Variable for Parent Product Category
-
I am trying to create a custom variable that displays the parent category of a custom taxonomy, in this case, WooCommerce product_cat. I got excited before I started seeing some weird behavior.
It’s kinda weird though, hoping someone can clarify because on some categories it doesn’t work. On the handful where it doesn’t display the parent it displays either the same category you are on (already in the title) or in one case it just picked a random child category to display.
Using %%parent_category%% to display using the code below.
Does anyone have any idea what’s going on?
function get_parent_category() { global $post; // Get the terms for the current post in your tax $myterms = get_the_terms( $post->ID, 'product_cat' ); // Check if parent exists if ($myterms[0]->parent !== 0){ // Get the term object $parent = get_term_by('id', $myterms[0]->parent, 'product_cat'); return $parent->name; } } function register_custom_yoast_variables() { wpseo_register_var_replacement( 'parent_category', 'get_parent_category', 'advanced', 'some help text' ); } add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Custom Variable for Parent Product Category’ is closed to new replies.