Shortcodes for multiple taxonomies

  • patrik
    patrik
    Newbie
     
    42 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.


    JamesD
    JamesD
    Elite Member
     
    554 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. :-)


    patrik
    patrik
    Newbie
     
    42 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?


    JamesD
    JamesD
    Elite Member
     
    554 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')

    JamesD
    JamesD
    Elite Member
     
    554 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.

    patrik
    patrik
    Newbie
     
    42 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?


    JamesD
    JamesD
    Elite Member
     
    554 points

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


    JamesD
    JamesD
    Elite Member
     
    554 points

    Perhaps this will help.

    Attachments:
    You must be logged in to view attached files.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.