• Resolved Eliot Akira

    (@miyarakira)


    Hello, this is not a question per se, just a note for anyone else who would like to insert a content block by name/slug using shortcode.

    I know there’s a handy “add content block” button above the editor, but using several content blocks within a page and only the IDs showing in the shortcode, it gets difficult to know which block is what. Inside post-widget.php, I changed the function custom_post_widget_shortcode this way:

    function custom_post_widget_shortcode( $atts ) {
    	extract( shortcode_atts( array(
    		'id' => '',
    		'name' => '',
    		'class' => 'content_block'
    	), $atts ) );
    
    	$content = "";
    
    	if ($id != "") {
    		$args = array(
    			'post__in' => array( $id ),
    			'post_type' => 'content_block',
    		);
    		$content_post = get_posts( $args );
    
    		foreach( $content_post as $post ) :
    			$content .= '<div class="'. esc_attr($class) .'">';
    			$content .= apply_filters( 'the_content', $post->post_content);
    			$content .= '</div>';
    		endforeach;
    	} else {
    		if ($name != "") {
    			$args = array(
    				'name' => array( $name ),
    				'post_type' => 'content_block',
    				  'post_status' => 'publish',
    				  'showposts' => 1,
    			);
    			$content_post = get_posts( $args );
    		foreach( $content_post as $post ) :
    			$content .= '<div class="'. esc_attr($class) .'">';
    			$content .= apply_filters( 'the_content', $post->post_content);
    			$content .= '</div>';
    		endforeach;
    		}
    	}
    	return $content;
    }

    Sorry for the inelegant code, I can’t figure out a nice way to avoid repetition. Anyway, now it’s possible to use a shortcode like [content_block name=”contact-info”] which helps to show what the block represents, instead of an anonymous ID number.

    I just want to add that this plugin has been very useful to me. There are other plugins for doing all kinds of things with custom post types, custom fields, taxonomies, etc., but this plugin gets the job done fast, and helped me to learn how custom post types work.

    https://www.ads-software.com/plugins/custom-post-widget/

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

    (@vanderwijk)

    Hi Eliot, thanks for the suggested code. Since there is already the possibility to select a content block by title when you use the button above the editor, I have decided not to implement this.

    See screenshot: https://i.imgur.com/5JJkj96.png

    Cassie

    (@cassiedong)

    Thanks for sharing the code snippets Eliot! We come across the same issue when utilising multiple Content Blocks on one page and are having real trouble figuring out which one is which. This is exactly what we needed.

    GermanKiwi

    (@germankiwi)

    Hi Johan, I would also love to see this feature implemented.

    I know that we can see the descriptive names of our content blocks in the “Insert Content Block” panel (as shown in your screenshot above), which is helpful when we want to insert a content block shortcode into a page.

    However, once the block shortcodes are already inserted into a page – and especially when there are multiple shortcodes on a single page – then it’s impossible to easily identify which shortcode belongs to which block, because they only reference the id number, not the slug or name.

    That’s why I think Eliot’s idea would be practical and incredibly helpful. If I can look at my page and see all the shortcodes looking like this:

    [content_block id=”my-block-slug-name”]

    …then I can instantly identify which shortcode is which – that’s a huge time saver! I myself have a couple of pages on my site that have multiple Content Block shortcodes listed in them.

    The ID number could still be used as well of course (important for backward compatibility). I have other plugins that use shortcodes, which support using either the ID number or the slug name in the shortcode – both are possible. Although I think it should default to the slug name for usability.

    I hope you will reconsider this and add support for it! There are three people in this thread here who would like to see this feature! ??

    Thanks!

    I would like to see this implemented as well. This is very useful. Please.

    Thread Starter Eliot Akira

    (@miyarakira)

    Just for reference: since the original post 4 months ago, I’ve written a plugin that displays content blocks (as well as fields, posts, widget areas, etc.) by name/slug: Custom Content Shortcode.

    It’s funny, I stumbled upon your plugin by accident “yesterday”, before you fired this email. I will definitely give it a try.

    I’d like to also vote for placed content blocks being identified with a slug rather than an ID code.

    […] Since there is already the possibility to select a content block by title when you use the button above the editor, I have decided not to implement this.

    Sure, it’s fine for placing by name, but once you have a few on a page the IDs are meaningless, and you can’t easily tell which one is which.

    Slugs would be much better.

    Thanks,
    Michael

    Plugin Author Johan van der Wijk

    (@vanderwijk)

    Hi Eliot, thanks for letting us know about your Custom Content Shortcode plugin, I am sure it will come in handy for many WordPress users!

    Hi Johan, can you kindly comment on whether you will add this requested functionality (slug names in the shortcode) to Custom Post Widget? I really hope you’ll consider it, based on our requests! ??

    Cheers!

    Even if you don’t amend the ‘Custom Post Widget’ plugin to place them by slug, it’d be nice for the overlay to drop the Content Block’s name into the short code even just as a label, so we can see what it is once it’s placed. It’s very confusing without some sort of name present, especially when you have multiple Content Blocks on the same page.

    Something like this perhaps: [content_block id=3329 title=’Form – Jobs’]

    Thanks,
    Michael

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Insert content block by name/slug using shortcode’ is closed to new replies.