0

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.

1
  • I'm unsure to how to use that exactly. – Darren Bachan May 30 '17 at 22:42
0

It seems like you are trying to get a term by it's slug or name. get_term() will get all the term's data by it's id. In this case, use get_term_by() instead:

function product_category_button($atts) {

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

    // if( $category ) :

        // Vars
        $taxonomy = 'product_categories';
        // Use 'name' instead of 'slug' if you want to get the term by it's name
        $term = get_term_by( 'slug' , $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' );

For further reading, take a look into codex.

0

use

get_category_link and get_category_by_slug

function product_category_button($atts) {

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

  // if( $category_slug) :
          $get_category = get_category_by_slug($category_slug);

          $link = get_category_link($get_category);

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


      return $html_out;

  // endif;
}

add_shortcode( 'see_all_products', 'product_category_button' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.