Shortcodes for multiple taxonomies

WordPress Business Themes Forums General Discussion Shortcodes for multiple taxonomies

Shortcodes for multiple taxonomies

December 2, 2017
patrik patrik
  • patrik
    patrik
    Newbie 44 points

    Taxonomies group “things together”.

    Lets say, I have

    taxonomies of colors:

    blue,red,white

    taxonomies of size

    S,M,L

    taxonomies of kind

    shoes,shirt,pants

    how can I display on a page

    blue, S pants

    or white L shoes

    THX.

    December 2, 2017 at 5:06 pm
  • JamesD
    JamesD
    Super Guru 912 points

    Hello patrik,
    Note: If this is above your comfort level then you may want to find someone to assist with it.

    As an example, you can add a shortcode function in your ‘_functions.php’ file:

    	function get_custprod_taxonomies($aAttr) {
    	    if( empty($aAttr['post_id']) ) { return ''; }
            $post_id = $aAttr['post_id'];
    	    $sReturn = '';
    	    $oTaxonomies = wp_get_post_terms($post_id, array('kind', 'size', 'colors'));
    	    foreach($oTaxonomies as $oTax ){
    	        $sTaxonomy = $oTax->taxonomy;
    	        $sValue = $oTax->name;
    	        switch($sTaxonomy) {
    	            case 'size':
    	                $sTitle = __('Size');
    	                break;
    	            case 'brand':
    	                $sTitle = __('Kind');
    	                break;
    	            case 'color':
    	                $sTitle = __('Color');
    	                break;
    	            default:
    	        }
    	        $sReturn .= sprintf('%s: %s<br />', $sTitle , $sValue);
    	    }
    	    return $sReturn;
    	}
    	add_shortcode('item_taxonomies', 'get_custprod_taxonomies');

    And change the display, titles and database keys (i.e. ‘kind’, ‘colors’, size’) to the actual values you need.

    Then to use the shortcode use the following in your template to call it:

    [item_taxonomies post_id="<?php echo $post->ID;?>"]

    Or, alternatively, you can hardcode the post ID like:

    [item_taxonomies post_id="34"]

    Note: Where you put it all depends on your theme and location, but this should point you in a decent direction.

    Hope this makes sense. :-)

    December 4, 2017 at 4:57 am
  • patrik
    patrik
    Newbie 44 points

    Thanks for the answer JamesD.

    Looking at the code, I assume that for every combination of the values of the “taxonomy combo” I would have to edit _functions.php?

    Or is there any way to call all 3 different values of 3 different taxonomies in on one shortcode to display it on one page?

    December 4, 2017 at 2:58 pm
  • JamesD
    JamesD
    Super Guru 912 points

    “Looking at the code, I assume that for every combination of the values of the “taxonomy combo” I would have to edit _functions.php?”

    Nope.

    “Or is there any way to call all 3 different values of 3 different taxonomies in on one shortcode to display it on one page?”

    That’s actually what this code does. For each post (that’s where the post_id comes in) the function returns the values of the taxonomies

    array('kind', 'size', 'colors')
    December 4, 2017 at 5:57 pm
  • JamesD
    JamesD
    Super Guru 912 points

    Hopefully, these images Hopefully, these images will help.

    Note: Ignore the second image (MultiTaxonomy2.jpg), has a typo.

    Attachments:
    You must be logged in to view attached files.
    December 4, 2017 at 7:44 pm
  • patrik
    patrik
    Newbie 44 points

    Yes, perfect, I undestand now logic behind the code, but if I would like to print on solitaire page “brown , shoes , 40”

    /because there will be adidas,nike,rebook “brands” taxonomy, but I dont care about that type of taxonomy/

    Is it possible to via shortcode?

    December 5, 2017 at 7:38 am
  • JamesD
    JamesD
    Super Guru 912 points

    Yes, the shortcode info is above, please re-read the post above

    December 5, 2017 at 5:06 pm
  • JamesD
    JamesD
    Super Guru 912 points

    Perhaps this will help.

    Attachments:
    You must be logged in to view attached files.
    December 5, 2017 at 5:53 pm
Viewing 8 posts - 1 through 8 (of 8 total)

Didn't find what you were after?

Chat with us now

or contact our support team