Skip to content

Internal Shortcode | [Post_Terms]

EkoJr edited this page Mar 17, 2017 · 5 revisions

Description

Adds (custom) taxonomy terms within the associated post/page. Displays both Post & Page types if taxonomy is added, but will only display terms from taxonomies set to public.

Usage

[post_terms taxonomy="category" delimiter=", " links="true" max="0" empty_message=""]

Params

taxonomy

(string) (required/optional) Determines which taxonomy to display terms from. Only one taxonomy can be used. WP Taxonomy Defaults Default: "category"

delimiter

(string) (optional) Adds a string to divide multiple terms. Default: ", "

links

(boolean) (optional) Determines whether to add a link to the term titles. Default: "true"

max

(integer) (optional) Sets the max amount of terms to display. "0" disables the limit. Default: "0"

empty_message

(string) (optional) Displays a message if no terms are used. Default: ""

Return

(string) Adds the Post's taxonomy terms (w/ links if true).

Examples/Sample

Example 1

[post_terms taxonomy="custom-taxonomy"]

Returns

<a href="example.com/custom-taxonomy/test-term-1/ >Test Term 1</a>, <a href="example.com/custom-taxonomy/test-term-2/ >Test Term 2</a>, <a href="example.com/custom-taxonomy/test-term-3/ >Test Term 3</a>

Example 2

[post_terms taxonomy="custom-taxonomy" links="false" max="2"]

Returns

Test Term 1, Test Term 2

Source

File: advanced-post-list/includes/class/apl_shortcodes.php

/**
 * Post Terms Shortcode. 
 * 
 * Desc: Adds (Custom) Taxonomy Terms associated with Post/Page. Displays both
 * post and page types, and will display terms from taxonomies that are set 
 * to public
 * 
 * 1. Get Taxonomy Terms if Taxonomy is valid, otherwise get (default) Categories. 
 * 2. Convert $atts['links'] to a boolean variable. 
 * 3. If terms exists format them to return, otherwise add an empty message 
 *    and do Step 6.
 * 4. Slice list of terms to max amount, and store array total and (i)ndex.
 * 5. For each term there is add the term name, w/ link if true, and add a
      delimiter except for last term.
 * 6. Return string.
 * 
 * @since 0.3.0 
 * @version 0.4.0 - Changed to Class function, and uses WP's built-in
 *                  functions for setting default attributes & do_shortcode().
 * 
 * @param array $atts {
 *      
 *      Shortcode Attributes. 
 *      
 *      @type string $taxonomy          [Req] Gets the terms from taxonomy, defaults to 
 *                                      'category'. 
 *      @type string $delimiter         Inserts a separator, default is ", ".
 *      @type boolean $links            Return as an HTML link if true.
 *      @type integer $max              Total amount to display. 
 *      @type string $empty_message     Display a message if no terms are returned.
 * }
 * @return string Taxonomy Terms used in post/page.
 */
public function post_terms($atts)
{
    //INIT
    $atts_value = shortcode_atts( array(
        'taxonomy' => 'category',
        'delimiter' => ', ',
        'links' => 'true',
        'max' => '0',
        'empty_message' => ''
    ), $atts, 'post_terms');
    $return_str = '';
    
    //STEP 1
    if (!taxonomy_exists($atts_value['taxonomy']))
    {
        $atts_value['taxonomy'] = 'category';
    }
    $terms = get_the_terms($this->_post->ID, $atts_value['taxonomy']);
    
    //STEP 2
    $links = TRUE;
    if (strtolower($atts_value['links']) == 'false')
    {
        $links = FALSE;
    }
    
    //STEP 3
    if ($terms)
    {
        //STEP 4
        $i = 1;
        $array_total = count($terms);
        if ($atts_value['max'] != '0')
        {
            if ($array_total > intval($atts_value['max']))
            {
                $terms = array_slice($terms, 0, intval($atts_value['max']));
                $array_total = count($terms);
            }
        }
        //STEP 5
        foreach ($terms as $term_key => $term)
        {
            if ($links)
            {
              $return_str .= '<a href="' . get_tag_link($term->term_id) . '" >' . $term->name . '</a>';
            }
            else
            {
              $return_str .= $term->name;
            }

            if ($array_total > $i)
            {
                $return_str .= $atts_value['delimiter'];
            }

            $i++;
        }
    }
    else
    {
        $return_str .= $atts_value['empty_message'];
    }
    
    //STEP 6
    return $return_str;
    
}

Additional Resources

Clone this wiki locally