WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Widget
[resolved] Insert content block by name/slug using shortcode (11 posts)

  1. Eliot Akira
    Member
    Posted 10 months ago #

    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.

    http://wordpress.org/plugins/custom-post-widget/

  2. Johan van der Wijk
    Member
    Plugin Author

    Posted 8 months ago #

    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: http://i.imgur.com/5JJkj96.png

  3. cassiedong
    Member
    Posted 7 months ago #

    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.

  4. GermanKiwi
    Member
    Posted 7 months ago #

    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!

  5. ragots
    Member
    Posted 6 months ago #

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

  6. Eliot Akira
    Member
    Posted 6 months ago #

    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.

  7. ragots
    Member
    Posted 6 months ago #

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

  8. mpmchugh
    Member
    Posted 6 months ago #

    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

  9. Johan van der Wijk
    Member
    Plugin Author

    Posted 6 months ago #

    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!

  10. GermanKiwi
    Member
    Posted 6 months ago #

    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!

  11. mpmchugh
    Member
    Posted 5 months ago #

    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

Reply

You must log in to post.

About this Plugin

About this Topic