Post Carousel Shortcode

Below, you’ll find a detailed guide on how to add the Post Carousel 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 Post Carousel Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Post Carousel Plugin and the shortcodes it provides:

Plugin Icon
Post Grid, Post Carousel, & List Category Posts – by Smart Post Show

"Post Grid, Post Carousel, & List Category Posts by Smart Post Show is a dynamic plugin used to create engaging layouts for your posts. It easily organizes content into grids, carousels, or lists."

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

Post Carousel [smart_post_show] Shortcode

The Smart Post Show shortcode is a versatile tool for displaying post carousels. It fetches a specific post carousel’s settings based on its ID, checks its existence and status, and retrieves its layout and view options. It also determines if the current page already contains the shortcode. If not, it enqueues necessary stylesheets and applies dynamic styles. Finally, it outputs the post carousel HTML.

Shortcode: [smart_post_show]

Parameters

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

  • id – The unique identifier for the specific post carousel

Examples and Usage

Basic example – A simple usage of the shortcode to display a post carousel with a specific id.

[smart_post_show id=1 /]

Advanced examples

Using the shortcode to display a post carousel with a specific id and applying a specific layout and view options. The carousel will load with the specified layout and view options.


// Add the layout and view options in the post meta
update_post_meta( 1, 'sp_pcp_layouts', 'layout1' );
update_post_meta( 1, 'sp_pcp_view_options', array( 'option1', 'option2' ) );

// Use the shortcode
echo do_shortcode( '[smart_post_show id=1 /]' );

Using the shortcode to display a post carousel with a specific id and applying a specific layout and view options. The carousel will load with the specified layout and view options. If the specified id is not found or the post status is ‘trash’, the shortcode will not render anything.


// Add the layout and view options in the post meta
update_post_meta( 2, 'sp_pcp_layouts', 'layout2' );
update_post_meta( 2, 'sp_pcp_view_options', array( 'option3', 'option4' ) );

// Use the shortcode
echo do_shortcode( '[smart_post_show id=2 /]' );

PHP Function Code

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

Shortcode line:

add_shortcode( 'smart_post_show', array( $this, 'pcp_shortcode_render' ) );

Shortcode PHP function:

function pcp_shortcode_render( $attribute ) {
		$shortcode_id = esc_attr( intval( $attribute['id'] ) );
		// Check the shortcode existence and status.
		if ( empty( $shortcode_id ) || 'sp_post_carousel' !== get_post_type( $shortcode_id ) || 'trash' === get_post_status( $shortcode_id ) ) {
			return;
		}
		// Preset Layouts.
		$layout = get_post_meta( $shortcode_id, 'sp_pcp_layouts', true );
		// All the visible options for the Shortcode like – Global, Filter, Display, Popup, Typography etc.
		$view_options  = get_post_meta( $shortcode_id, 'sp_pcp_view_options', true );
		$section_title = get_the_title( $shortcode_id );

		// Get the existing shortcode id from the current page.
		$get_page_data      = self::get_page_data();
		$found_shortcode_id = $get_page_data['generator_id'];
		ob_start();
		// Check if shortcode and page ids are not exist in the current page then enqueue the stylesheet.
		if ( ! is_array( $found_shortcode_id ) || ! $found_shortcode_id || ! in_array( $shortcode_id, $found_shortcode_id ) ) {
			wp_enqueue_style( 'font-awesome' );
			wp_enqueue_style( 'pcp_swiper' );
			wp_enqueue_style( 'pcp-style' );
			$dynamic_style = self::load_dynamic_style( $shortcode_id, $view_options, $layout );
			// Add dynamic style.
			echo '<style id="sp_pcp_dynamic_style' . esc_attr( $shortcode_id ) . '">' . $dynamic_style['dynamic_css'] . '</style>'; // phpcs:ignore
		}
		// Update options if the existing shortcode id option not found.
		self::sps_db_options_update( $shortcode_id, $get_page_data );
		SP_PC_Output::pc_html_show( $view_options, $layout, $shortcode_id, $section_title );
		return ob_get_clean();
	}

Code file location:

post-carousel/post-carousel/public/class-smart-post-show-public.php

Conclusion

Now that you’ve learned how to embed the Post Carousel 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 *