Title: Insert title in shortcode
Last modified: August 22, 2016

---

# Insert title in shortcode

 *  Resolved [mearleycf](https://wordpress.org/support/users/mearleycf/)
 * (@mearleycf)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/insert-title-in-shortcode/)
 * Are there other tags for the shortcode, like displaying the title? I want to 
   insert the content block on a page with its title and content both, but right
   now I only know how to insert the content using the insert content block button
   on the post editor. What is the tag for including the title?
 * [https://wordpress.org/plugins/custom-post-widget/](https://wordpress.org/plugins/custom-post-widget/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Johan van der Wijk](https://wordpress.org/support/users/vanderwijk/)
 * (@vanderwijk)
 * [11 years, 5 months ago](https://wordpress.org/support/topic/insert-title-in-shortcode/#post-5602722)
 * Hi mearleycf,
 * Currently it is not possible to include the post title when using the shortcode.
   I shall add this to my todo list (code suggestions are highly appreciated!).
 *  [hgh001](https://wordpress.org/support/users/hgh001/)
 * (@hgh001)
 * [10 years, 5 months ago](https://wordpress.org/support/topic/insert-title-in-shortcode/#post-5602952)
 * I would like to “upvote” this as well. I wrote my own shortcode function based
   on yours to include this and some other improvements in the attribute handling(
   see [this helpful thread](http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode)).
 * See what you think (and feel free to use all or some):
 *     ```
       function my_custom_post_widget_shortcode( $atts ) {
           $args = shortcode_atts( array(
               'id' => '-1',
               'slug' => '',
               'class' => 'content_block',
               'suppress_content_filters' => 'no',
               'title' => 'no',
               'title_tag' => 'h2'
           ), $atts, 'custom_post_widget_shortcode' );
   
           $suppress_content_filters = filter_var($args['suppress_content_filters'], FILTER_VALIDATE_BOOLEAN);
           $show_title = filter_var($args['title'], FILTER_VALIDATE_BOOLEAN);
   
           $query_args = array(
               'post_type' => 'content_block'
           );
   
           if ( !empty($args['slug']) ) {
               $query_args['name'] = $args['slug'];
           } else {
               $query_args['p'] = $args['id'];
           }
   
           $output = "";
           $content_post = get_posts( $query_args );
   
           foreach( $content_post as $post ) :
               $output .= '<div class="'. esc_attr($args['class']) .'" id="custom_post_widget-' . $id . '">';
               if ($show_title) {
                   $output .= '<'.$args['title_tag'].'>';
                   if ($suppress_content_filters) {
                       $output .= $post->post_title;
                   } else {
                       $output .= apply_filters('the_title', $post->post_title);
                   }
                   $output .= '</'.$args['title_tag'].'>';
               }
               if ( $suppress_content_filters ) {
                   $output .= $post->post_content;
               } else {
                   $output .= apply_filters( 'the_content', $post->post_content);
               }
               $output .= '</div>';
           endforeach;
   
           return $output;
       }
       ```
   
 * For anyone who would like to use this, simply override the shortcode in your 
   own plugin or theme. I found the ‘wp_loaded’ action worked fine, but will admit
   there may be a better choice – you just have to make sure the plugins are loaded
   and activated:
 *     ```
       function override_cpw_shortcode(){
           add_shortcode( 'content_block', 'my_custom_post_widget_shortcode' );
       }
       add_action('wp_loaded', 'override_cpw_shortcode');
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Insert title in shortcode’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-widget/assets/icon.svg?rev=2884166)
 * [Content Blocks (Custom Post Widget)](https://wordpress.org/plugins/custom-post-widget/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-widget/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-widget/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-widget/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-widget/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-widget/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [hgh001](https://wordpress.org/support/users/hgh001/)
 * Last activity: [10 years, 5 months ago](https://wordpress.org/support/topic/insert-title-in-shortcode/#post-5602952)
 * Status: resolved