Skip to content

Internal Shortcode | [Post_Parent]

EkoJr edited this page Mar 17, 2017 · 2 revisions

Description

Adds the 'Page' parent (WP_Query uses Post as a common Post/Page object).

Usage

[post_parent link="true"]

Params

link

(string) (optional) Determines whether or not to add a link to the (Parent) Page Title. Default: "true"

Return

(string) Adds Page Parent ( w/ link).

Examples/Sample

Example 1

[post_parent]

Returns

<a href="LINK" >PAGE TITLE</a>

Source

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

/**
 * Page Parent Shortcode. 
 * 
 * Desc: Adds Page Parent Title, and optionally with link.
 * 
 * 1. Check to see if Post Parent is set. 
 * 2. Grab Post Parent Title w/ Link if set to 'true'. 
 * 3. 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().
 * 
 * @param array $atts {
 *      
 *      Shortcode Attributes. 
 *      
 *      @type string $link      Returns as html link if true. 
 * }
 * @return string Post Parent Title OR as html link.
 */
public function post_parent($atts)
{
    //INIT
    $atts_value = shortcode_atts( array(
        'link' => 'true'
    ), $atts, 'post_parent');
    $return_str = '';
    
    //STEP 1
    if ($this->_post->post_parent != 0)
    {
        //STEP 2
        if (strtolower($atts_value['link']) == "false")
        {
            $return_str .= get_the_title($this->_post->post_parent);
        }
        else
        {
            $return_str .= '<a href="' . get_permalink($parentID) . '" >' . get_the_title($parentID) . '</a>';
        }
    }
    
    //STEP 3
    return $return_str;
}

Additional Resources

Clone this wiki locally