Skip to content

Internal Shortcode | [Post_Excerpt]

EkoJr edited this page Jan 22, 2017 · 2 revisions

Description

Adds the Post's Excerpt, a short description, that is much like [post_content], but is limited by its length.

Usage

[post_excerpt length="250"]

Params

length

(integer) (optional) Determines the string length that will be returned/added. Default: "250"

Return

(string) Post Excerpt.

Examples/Sample

Example 1

[post_excerpt]

Returns

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It

Example 2

[post_excerpt length="50"]

Returns

Lorem Ipsum is simply dummy text of the printing

Source

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

/**
 * Post Excerpt Shortcode. 
 * 
 * Desc: Adds the Post Excerpt, or a substring of Post Content.
 * 
 * 1. IF Post_Excerpt is empty, then use Post_Content with X amount of 
 *    characters (Default 250.). Otherwise skip to STEP 4.
 * 2. Convert 'length' Variable Type to Int.
 * 3. Add a substring from Post_Content to return. X amount of length and
 *    with shortcodes stripped out.
 * 4. Add Post_Excerpt to return.
 * 5. Return String.
 * 
 * @since 0.1.0 
 * @version 0.4.0 - Changed to Class function, and uses WP's built-in
 *                  functions for setting default attributes & do_shortcode().
 *                  Also changed substr() to mp_substr() for encoding compatability.
 * 
 * @param array $atts {
 *      
 *      Shortcode Attributes. 
 *      
 *      @type string $length    Sets the character length.  
 * }
 * @return string Post->Excerpt OR Post->Post_Content substring.
 */
public function post_excerpt($atts)
{
    //INIT
    $atts_value = shortcode_atts( array(
        'length' => '250'
    ) , $atts, 'post_excerpt');
    $return_str = '';
    
    //STEP 1
    if(empty($this->_post->post_excerpt))
    {
        //STEP 2 
        if (is_numeric($atts_value['length']))
        {
            $atts_value['length'] = intval($atts_value['length']);
        }
        else
        {
            $atts_value['length'] = 250;
        }
        
        //STEP 3
        $encoding = mb_internal_encoding();
        $return_str = strip_shortcodes(mb_substr(strip_tags($this->_post->post_content),
                                                 0,
                                                 $atts_value['length'],
                                                 $encoding));
    }
    
    //STEP 4
    else
    {
        $return_str .= $this->_post->post_excerpt;
    }
    
    //STEP 5
    return $return_str;
}

Additional Resources

Clone this wiki locally