Related Posts Thumbnails for WordPress Shortcode

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

Before starting, here is an overview of the Related Posts Thumbnails for WordPress Plugin and the shortcodes it provides:

Plugin Icon
Related Posts Thumbnails Plugin for WordPress

"Related Posts Thumbnails Plugin for WordPress is a user-friendly tool designed to enhance your site by displaying related posts with attractive thumbnails. Boost engagement and user retention effortlessly!"

★★★★☆ (206) Active Installs: 30000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [related-posts-thumbnails]

Related Posts Thumbnails for WordPress [related-posts-thumbnails] Shortcode

The Related Posts Thumbnails shortcode is a powerful tool that displays related posts with their thumbnails. It allows customization of the number of posts, sorting method, and main title. The shortcode also provides an option to exclude specific posts. It verifies the input values for safety and defaults to preset values if the input is invalid.

Shortcode: [related-posts-thumbnails]

Parameters

Here is a list of all possible related-posts-thumbnails shortcode parameters and attributes:

  • posts_number – determines the number of related posts to display
  • posts_sort – sets the order of the displayed posts, either randomly or by the latest
  • main_title – allows you to set a custom title for the section of related posts
  • exclude_post – lets you specify certain posts to exclude from the related posts list

Examples and Usage

Basic example – The following shortcode displays the related posts thumbnails with the default settings. It will display three thumbnails in a random order.

[related-posts-thumbnails /]

Advanced examples

Displaying five related posts thumbnails in the order of their publishing date. This shortcode also excludes the post with the ID ‘2’ from being displayed.

[related-posts-thumbnails posts_number=5 posts_sort=latest exclude_post=2 /]

Displaying three related posts thumbnails in a random order with a main title of ‘Related Articles’. This shortcode also excludes the posts with the IDs ‘2’ and ‘3’ from being displayed.

[related-posts-thumbnails main_title=Related_Articles exclude_post=2,3 /]

Displaying three related posts thumbnails in a random order with a main title of ‘You Might Also Like’. This shortcode does not exclude any posts.

[related-posts-thumbnails main_title=You_Might_Also_Like /]

Please note that the ‘main_title’ parameter should be entered with underscores (‘_’) instead of spaces. Also, if you want to exclude more than one post, you should separate the IDs with commas (‘,’).

PHP Function Code

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

Shortcode line:

add_shortcode( 'related-posts-thumbnails', array( $this, 'related_posts_shortcode' ) );

Shortcode PHP function:

function related_posts_shortcode( $atts ) {

		$atts = shortcode_atts( array(
			'posts_number'	=> '3',
			'posts_sort'	=> 'random',
			'main_title'	=> '',
			'exclude_post'		=> '' ), 
			$atts, 'related-posts-thumbnails'
		);

		$number = $atts['posts_number'];

		if ( $atts['posts_sort'] == 'random' ) {
			$sort = 'rand()';
		} elseif ( $atts['posts_sort'] == 'latest' ) {
			$sort = 'post_date';
		}

		//sanitization through regex expression to know if a string is consisting of numeric values.
		$regex = '/^\d+(?:,\d+)*$/';
		$excluded_posts_array =  preg_match( $regex, $atts['exclude_post'] ) ? $atts['exclude_post'] : array();

		if ( !is_numeric( $number ) ) {
			$number = 3;
		}

		$main_title = str_replace( '_', ' ', $atts['main_title'] );

		return $this->get_thumbnails( true, $number, $sort, $main_title, $excluded_posts_array );

	}

Code file location:

related-posts-thumbnails/related-posts-thumbnails/related-posts-thumbnails.php

Conclusion

Now that you’ve learned how to embed the Related Posts Thumbnails for WordPress 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 *