Skip to content

Internal Shortcode | [Post_Type]

EkoJr edited this page Mar 17, 2017 · 2 revisions

Description

Adds the Post Type label a given post belongs to.

Usage

[post_type label="name"]

Params

label

(string) (optional) Determines which label to display from the Post Type object. Default: "name"

  • 'name' => 'Posts'
  • 'singular_name' => 'Post'
  • 'add_new' => 'Add New post'
  • 'add_new_item' => 'Add New Post'
  • 'edit_item' => 'Edit Post'
  • 'new_item' => 'New Post'
  • 'view_item' => 'View Post'
  • 'view_items' => 'View Posts'
  • 'search_items' => 'Search Posts'
  • 'not_found' => 'No posts found.'
  • 'not_found_in_trash' => 'No posts found in Trash.'
  • 'parent_item_colon' => 'Parent Page:'
  • 'all_items' => 'All Posts'
  • 'archives' => 'Post Archives'
  • 'attributes' => 'Post Attributes'
  • 'insert_into_item' => 'Insert into post'
  • 'uploaded_to_this_item' => 'Uploaded to this post'
  • 'featured_image' => 'Featured Image'
  • 'set_featured_image' => 'Set featured image'
  • 'remove_featured_image' => 'Remove featured image'
  • 'use_featured_image' => 'Use as featured image'
  • 'filter_items_list' => 'Filter posts list'
  • 'items_list_navigation' => 'Posts list navigation'
  • 'items_list' => 'Posts list'

Return

(string) Adds Post's Post Type.

Examples/Sample

Example 1

[post_type]

Returns

Posts

Source

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

/**
 * Post Type Shortcode. 
 * 
 * Desc: Adds the Post Type (Label) Name associated with the post/page ID.
 * 
 * 1. Get Post Type Object associated with the post. 
 * 2. Get the set label from object, otherwise get name. 
 * 3. Return string.
 * 
 * @since 0.4.0
 * 
 * @link https://codex.wordpress.org/Function_Reference/get_post_type_object
 * 
 * @param array $atts {
 *      
 *      Shortcode Attributes. 
 *      
 *      @type string $label     Gets post_type->labels, or defaults to 'name'.
 * }
 * @return string Post Type (Label) Name.
 */
public function post_type($atts)
{
    $atts_value = shortcode_atts( array(
        'label' => 'name'
    ), $atts, 'post_type');
    $return_str = '';
    
    //STEP 1
    $post_type_obj = get_post_type_object($this->_post->post_type);
    
    //STEP 2
    $label = $atts_value['label']; 
    if ( isset($post_type_obj->labels->$label) )
    {
        $return_str .= $post_type_obj->labels->$label;
    }
    else if ( isset($post_type_obj->labels->name) )
    {
        $return_str .= $post_type_obj->labels->name;
    }
    
    //STEP 3
    return $return_str;
}

Additional Resources

Clone this wiki locally