• 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

    (@vanderwijk)

    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?

    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom block shortcode template’ is closed to new replies.