Content Blocks (Custom Post Widget) Shortcode

Below, you’ll find a detailed guide on how to add the Content Blocks (Custom Post Widget) Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Content Blocks (Custom Post Widget) Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Content Blocks (Custom Post Widget) Plugin and the shortcodes it provides:

Plugin Icon
Content Blocks (Custom Post Widget)

"Content Blocks (Custom Post Widget) is a handy WordPress plugin that lets you create reusable widget content from your posts. You can display these blocks anywhere on your site."

★★★★☆ (76) Active Installs: 20000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [content_block]

Content Blocks (Custom Post Widget) [content_block] Shortcode

The ‘content_block’ shortcode from the Custom Post Widget plugin is used to display a custom post or content block on your site. This shortcode accepts several parameters like ‘id’, ‘slug’, ‘class’, etc. If a ‘slug’ is provided, it fetches the content block with that slug. It also allows for customization of the output through a template file. The content can include the post title, featured image, and the post content itself, based on the parameters set. The output is wrapped in a specified HTML tag with a class for easy styling.

Shortcode: [content_block]

Parameters

Here is a list of all possible content_block shortcode parameters and attributes:

  • id – Unique identifier of the content block.
  • slug – URL-friendly version of the content block’s name.
  • class – CSS class applied to the content block.
  • suppress_content_filters – Determines if content filters are applied. ‘no’ applies filters.
  • featured_image – If set to ‘yes’, the featured image of the content block is displayed.
  • featured_image_size – Defines the size of the featured image. Default is ‘medium’.
  • title – If ‘yes’, the content block’s title is displayed.
  • title_tag – HTML tag used for the content block’s title. Default is ‘h3’.
  • markup – The HTML tag used to wrap the content block. Default is ‘div’.
  • template – Name of the template file used for displaying the content block.

Examples and Usage

Basic example – Display a content block by referencing its ID

[content_block id="123" /]

Advanced examples

Display a content block by referencing its slug. This is useful when you want to reference a block by its name instead of its ID.

[content_block slug="my-custom-block" /]

Display a content block with a custom CSS class. This can be useful for styling the block in a specific way.

[content_block id="123" class="my-custom-class" /]

Display a content block with its featured image. This can be useful for blocks that contain a featured image you want to display.

[content_block id="123" featured_image="yes" /]

Display a content block with its title. This can be useful for blocks where you want to display the title of the block.

[content_block id="123" title="yes" /]

Suppress content filters for a content block. This can be useful for blocks where you want to display the raw content without any WordPress filters being applied.

[content_block id="123" suppress_content_filters="yes" /]

Display a content block using a specific template. This can be useful for blocks where you want to use a custom template to display the block.

[content_block id="123" template="my-custom-template.php" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [content_block] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'content_block', 'custom_post_widget_shortcode' );

Shortcode PHP function:

function custom_post_widget_shortcode( $atts ) {
	$params = shortcode_atts( array(
		'id' => '',
		'slug' => '',
		'class' => 'content_block',
		'suppress_content_filters' => 'no',
		'featured_image' => 'no',
		'featured_image_size' => 'medium',
		'title' => 'no',
		'title_tag' => 'h3',
		'markup' => 'div',
		'template' => ''
	), $atts );

	$id = $params['id'];
	$slug = $params['slug'];
	$class = $params['class'];
	$suppress_content_filters = $params['suppress_content_filters'];
	$featured_image = $params['featured_image'];
	$featured_image_size = $params['featured_image_size'];
	$title = $params['title'];
	$title_tag = $params['title_tag'];
	$markup = $params['markup'];
	$template = $params['template'];

	if ( $slug ) {
		$block = get_page_by_path( $slug, OBJECT, 'content_block' );
		if ( $block ) {
			$id = $block->ID;
		}
	}

	$content = "";

	// Attempt to load a template file
	if ( $params['template'] != '' ) {
		if ( $located = locate_template( $params['template'] ) ) {
			include_once $located;
		}
	}

	if ( $id != "" ) {

		$args = array(
			'post__in' => array( $id ),
			'post_type' => 'content_block',
		);

		$content_post = get_posts( $args );

		foreach( $content_post as $post ) :

			if ( isset( $located ) ) {
				// Template-based content
				$content .= call_user_func( 'shortcode_template', $post );

			} else {
				// Standard format content
				$content .= '<' . esc_attr( $markup ) . ' 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 ( $featured_image === 'yes' ) {
					$content .= get_the_post_thumbnail( $post -> ID, $featured_image_size );
				}
				if ( $suppress_content_filters === 'no' ) {
					$content .= apply_filters( 'the_content', $post -> post_content );
				} else {
					$content .= $post -> post_content;
				}
				$content .= '</' .  esc_attr( $markup ) . '>';
			}
		endforeach;

	}

	return $content;
}

Code file location:

custom-post-widget/custom-post-widget/shortcode.php

Conclusion

Now that you’ve learned how to embed the Content Blocks (Custom Post Widget) Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *