Title: Custom block shortcode template
Last modified: August 6, 2017

---

# Custom block shortcode template

 *  [maettyhawk](https://wordpress.org/support/users/maettyhawk/)
 * (@maettyhawk)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/custom-block-shortcode-template/)
 * Wouldn’t it be nice to have the option to output custom HTML when using shortcodes?
   Based on the possibility to change the widget layout I’d suggest to enhance post-
   widget.php like in the following:
 *     ```
       function custom_post_widget_shortcode( $atts ) {
       	extract( shortcode_atts( array(
       		'id' => '',
       		'slug' => '',
       		'class' => 'content_block',
       		'suppress_content_filters' => 'no',
       		'title' => 'no',
       		'title_tag' => 'h3'
       	), $atts ) );
   
       	if ( $slug ) {
       		$block = get_page_by_path( $slug, OBJECT, 'content_block' );
       		if ( $block ) {
       			$id = $block->ID;
       		}
       	}
   
       	$content = "";
   
       	if( $id != "" ) {
       		$args = array(
       			'post__in' => array( $id ),
       			'post_type' => 'content_block',
       		);
   
       		$content_post = get_posts( $args );
   
       		if ( $located = locate_template( 'custom-post-shortcode.php' ) ) {
       			require $located;
       			return;
       		}
   
       		foreach( $content_post as $post ) :
       			$content .= '<div class="'. esc_attr($class) .'" id="custom_post_widget-' . $id . '">';
       			if ( $title === 'yes' ) {
       				$content .= '<' . esc_attr( $title_tag ) . '>' . $post->post_title . '</' . esc_attr( $title_tag ) . '>';
       			}
       			if ( $suppress_content_filters === 'no' ) {
       				$content .= apply_filters( 'the_content', $post->post_content);
       			} else {
       				$content .= $post->post_content;
       			}
       			$content .= '</div>';
       		endforeach;
       	}
   
       	return $content;
       }
       add_shortcode( 'content_block', 'custom_post_widget_shortcode' );
       ```
   
 * A custom shortcode template:
 *     ```
       <?php
   
       foreach( $content_post as $post ) :
           $content .= '<div class="'. esc_attr($class) .'" id="custom_post_widget-' . $id . '">';
           if ( $title === 'yes' ) {
               $content .= '<' . esc_attr( $title_tag ) . '>' . $post->post_title . '</' . esc_attr( $title_tag ) . '>';
           }
           if ( $suppress_content_filters === 'no' ) {
               $content .= apply_filters( 'the_content', $post->post_content);
           } else {
               $content .= $post->post_content;
           }
           $content .= '</div>';
       endforeach;
   
       echo $content
   
       ?>
       ```
   

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

 *  Plugin Author [Johan van der Wijk](https://wordpress.org/support/users/vanderwijk/)
 * (@vanderwijk)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/custom-block-shortcode-template/#post-9740613)
 * Hi Maetty, I see you used the functionality for using a custom template when 
   using the a widget but I don’t think that this works for shortcodes. Or have 
   you managed to get this to work?
 *  [mofo.is.guilty](https://wordpress.org/support/users/mofoisguilty/)
 * (@mofoisguilty)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/custom-block-shortcode-template/#post-10090170)
 * Hi Johan,
 * I was browsing this board just to suggest you a very similar upgrade.
 * Your plugin actually allows template overriding, but only when the content block
   is added as a widget and not as a shortcode.
 * I’d suggest you to wrap the template handler you are using in **function widget()**
   to something like **function cpw_template_handler()** so you can call that in**
   function custom_post_widget_shortcode()** as well.
 * bb
    mofo
    -  This reply was modified 8 years, 2 months ago by [mofo.is.guilty](https://wordpress.org/support/users/mofoisguilty/).

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

The topic ‘Custom block shortcode template’ 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: [mofo.is.guilty](https://wordpress.org/support/users/mofoisguilty/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/custom-block-shortcode-template/#post-10090170)
 * Status: not resolved