Skip to content

Internal Shortcode | [Post_Meta]

EkoJr edited this page Feb 24, 2017 · 3 revisions

Description

Adds post meta data used within associated post/page.

Usage

[post_meta name=""]

Params

name

(string) (required/optional) Meta Name/Label. Default: ""

Return

(string) Meta data values, OR nothing if no meta variable is found.

Examples/Sample

Example 1

[post_meta name="foo"]

Returns

bar

Source

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

/**
 * Post Meta Shortcode. 
 * 
 * Desc: Adds Post MetaData used within posts/pages. Returns empty if nothing
 * is found.
 * 
 * 1. If post_meta 'name' is valid then get MetaData and add to return, 
 *    otherwise skip to Step 2.
 * 2. 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 $name      Meta Name/Label used within post.
 * }
 * @return string Taxonomy Terms used in post/page.
 */
public function post_meta($atts)
{
    //INIT
    $atts_value = shortcode_atts( array(
        'name' => ''
    ), $atts, 'post_meta');
    $return_str = '';
    
    //STEP 1
    if(!empty($atts_value['name']) && metadata_exists('post', $this->_post->ID, $atts_value['name']))
    {
        $post_meta_arr = get_post_meta($this->_post->ID, $atts_value['name'], FALSE);
        foreach ($post_meta_arr as $meta)
        {
            $return_str .= $meta;
        }
    }
    //TODO ADD Else Alert to Admin that metadata is invalid or doesn't exist.
    
    //STEP 2
    return $return_str;
    
}

Additional Resources

Clone this wiki locally