Skip to content

Internal Shortcode | [Item_Number]

EkoJr edited this page Mar 17, 2017 · 2 revisions

Description

Adds an index, numeric value, to each item in a (APL) post list.

Usage

[item_number offset="1" increment="1"]

Params

offset

(integer) (optional) Sets which numeric value to start from. Default: "1"

increment

(integer) (optional) How many to add each loop. Default: "1"

Return

(string) A numeric value.

Examples/Sample

Example 1

[item_number]

Returns

1 ... 2 ... 3 ...etc

Source

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

/**
 * Post List Item Number Shortcode. 
 * 
 * Desc: Adds a Numeric value to each post; by X amount of increments, and 
 * starting from where it is offset as. 
 * 
 * 1. If any non-digits are present, reset to default. 
 * 2. Convert variables to integer types. 
 * 3. Add item number with increment and added offset. 
 * 4. 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 integer $offset       Digit to start from. 
 *      @type integer $increment    Amount to increase by. 
 * }
 * @return string Index value. 
 */
public function item_number()
{
    //INIT
    $atts_value = shortcode_atts( array(
        'offset' => '1',
        'increment' => '1'
    ), $atts, 'item_number');
    $return_str = '';
    
    //STEP 1
    if (!is_numeric($atts_value['increment']))
    {
        $atts_value['increment'] = '1';
    }
    if (!is_numeric($atts_value['offset']))
    {
        $atts_value['offset'] = '1';
    }
    //STEP 2
    $atts_value['increment'] = intval($atts_value['increment']);
    $atts_value['offset'] = intval($atts_value['offset']);
    
    //STEP 3
    $return_str .= (string) (($this->_item_count * $atts_value['increment']) + $atts_value['offset']);
    
    //STEP 4
    return $return_str;
}

Additional Resources

Clone this wiki locally