Home Forums General Issues Changing a shortcode so it links an individual product based on its taxonomy


Changing a shortcode so it links an individual product based on its taxonomy

  • On my website I have a “Products” custom post type, that lists different Amazon products. There are various product categories (mouse, keyboard, monitor etc), which I differentiate using a custom taxonomy (“product_category”).

    To display the products on the frontend, I use an ACF “post object” field type to select an individual product, and output them in the form of a text link. The shortcode I am using to achieve this is:

    global $wpdb;
        $faqTitleUrl = "";
        $faqProData = shortcode_atts( array(
                'field' => 'what_controller'
    ,            ), $atts );
         $faqPostID = get_field( $faqProData['field'] );
         if($faqPostID !=''){
         $faqPostTitle = get_the_title($faqPostID);
         $faqPostUrl   = get_field('product_link',$faqPostID);
         $faqProductTitleUrl = '<a href="'.$faqPostUrl.'" target="_blank">'.$faqPostTitle.'</a>';
        return $faqProductTitleUrl;
    add_shortcode('faq_product', 'faq_product');

    I then use [faq_product field=”FIELD_NAME”] shortcode to display the link. This is working fine, but only when the ACF post object field is set up to display a single entry.

    What I would like to do is turn on ACFs “Select multiple values?” feature to allow me to have multiple products selected, and then edit the above code so it is able to display only one of these product links, based on the category I choose.

    As an example, my post object field has the following products:

    | Product 1 (Mouse Category) | | Product 2 (Keyboard category) | | Product 3 (Monitor category) |

    If I wanted to just display the link to the mouse on the frontend, I could use a shortcode, with something along the lines of [faq_product cat=”MOUSE” field=”FIELD_NAME”]

    I’d really appreciate it if someone could help edit the shortcode to allow for this.

    Many thanks.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.