• Resolved deandid

    (@lcm404)


    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)
  • Plugin Support Maybellyne

    (@maybellyne)

    Hello,

    Thanks for reaching out.

    To provide more clarity, can you please provide a screenshot of the snippet preview and the URL of the site?

    Thread Starter deandid

    (@lcm404)

    Not sure I am clear on what the snippet preview is? My boss won’t let me share links on this support forum.

    Plugin Support Md Mazedul Islam Khan

    (@mazedulislamkhan)

    We understand your concern about sharing the screenshot of the snippet preview. The snippet preview in the Yoast SEO plugin is the one that appears on the edit page where the plugin shows a preview of how the site will appear in both Desktop and Mobile search results.

    That said, as much as we hate to say it, your question goes beyond the technical expertise of me and my colleagues on the Yoast support team. This means we can’t give you a solid answer to your question.

    But we don’t want to leave you empty-handed either, that’s why we’ve created our developer portal at developer.yoast.com. This has proven to be useful to many developers in the past and hopefully will serve you well too.

    Sorry, we can’t be of more help here. However, we’ll leave the thread open for a week so someone from the community might help you who’s more familiar with it.

    Thread Starter deandid

    (@lcm404)

    Ok, thank you.

    Plugin Support devnihil

    (@devnihil)

    @lcm404 You’re welcome. Please let us know if you require any further assistance.

    Thread Starter deandid

    (@lcm404)

    All is good here. I found someone to dig in and address.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Variable for Parent Product Category’ is closed to new replies.