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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Custom block shortcode template’ is closed to new replies.