Site icon Hip-Hop Website Design and Development

how do you get one specific term from a shortcode attribute?

I’m working on a function where I just want to output a button that links to a specific term. I have used get_the_terms and have gotten it to work successfully, but I’ve been trying get_term and I’ve have no luck. I want the shortcode to look like this [see_all_products category="bakery"] and output a button that links to it.

This is my function so far:

function product_category_button($atts) {

    extract(shortcode_atts(array(
        'category'  => '',
    ), $atts));

    // if( $category ) :

        // Vars
        $taxonomy = 'product_categories';
        $term = get_term( $category, $taxonomy ); //$post->ID
        $cat = $term->name;
        $parent = $term->parent;

        var_dump($cat);

        if ($parent == NULL) :

            // Vars
            $link = get_term_link( $term->slug, $taxonomy);

            $html_out = '';
            $html_out .= '<a class="x-btn x-btn-primary x-btn-global hvr-bounce-to-bottom" href="' . $link . '">' . 'See All Products' . $cat . '</a>';
        endif;

        return $html_out;

    // endif;
}

add_shortcode( 'see_all_products', 'product_category_button' );

Right now, $link gives me this error “Catchable fatal error: Object of class WP_Error could not be converted to string” and $cat returns NULL in the var_dump.

Not sure if this could be affecting it, but the $parent stuff here was meant to get only the parent term.