• Resolved mearleycf

    (@mearleycf)


    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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Johan van der Wijk

    (@vanderwijk)

    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!).

    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).

    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.