Random Content Shortcodes

Below, you’ll find a detailed guide on how to add the Random Content Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Random Content Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Random Content Plugin and the shortcodes it provides:

Plugin Icon
Random Content

"Random Content is a WordPress plugin that enriches your website by generating and displaying unique, random content on demand, keeping your pages fresh and engaging."

★★★★☆ (31) Active Installs: 4000+ Tested with: 6.2.3 PHP Version: false
Included Shortcodes:
  • [random]
  • [random_content]

Random Content [random] Shortcode

The Random Content shortcode is a dynamic function in WordPress that displays random posts. It filters results based on group ID. If a group ID is set, it will only show posts from that group. If not, it will display random posts from all entries. The number of posts displayed can be adjusted. Shortcode: [random group_id=”” num_posts=”1″] The shortcode ends by either returning the post content or a ‘No posts found’ message if no posts match the criteria.

Shortcode: [random]

Parameters

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

  • group_id – The identifier of a specific group to filter posts from.
  • num_posts – The number of posts to display from the selected group or from all posts.

Examples and Usage

Basic example – The shortcode displays a single random post without any specific group.

[random num_posts=1]

Advanced examples

Displaying three random posts from a specific group. The group is identified by the group_id attribute.

[random group_id=2 num_posts=3]

Displaying five random posts without any specific group. The number of posts is defined by the num_posts attribute.

[random num_posts=5]

Displaying a single random post from a specific group. The group is identified by the group_id attribute.

[random group_id=3]

PHP Function Code

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

Shortcode line:

add_shortcode('random', array(&$this, 'shortcode'));

Shortcode PHP function:

function shortcode($atts)
	{
		$a = shortcode_atts(array(
			'group_id' => '',
			'num_posts' => 1,
		), $atts);

		// if $group_id is set, then filter results by $group_id
		if (!empty($a['group_id'])) {

			$my_query = new WP_Query(array(
				'post_type' => 'endo_wrc_cpt',
				'posts_per_page' => $a['num_posts'],
				'orderby' => 'rand',
				'tax_query' => array(
					array(
						'taxonomy' => 'endo_wrc_group',
						'field' => 'id',
						'terms' => $a['group_id']
					)
				)
			));
		} else {

			// filter through all entries
			$my_query = new WP_Query(array(
				'post_type' => 'endo_wrc_cpt',
				'posts_per_page' => $a['num_posts'],
				'orderby' => 'rand'
			));
		}

		if ($my_query->have_posts()) {

			$content = "";

			while ($my_query->have_posts()) : $my_query->the_post();

				$content .= apply_filters('the_content', get_the_content());

			endwhile;
		} else {
			$content = __('No posts found.', 'random-content');
		}

		wp_reset_postdata();

		return $content;
	}

Code file location:

random-content/random-content/class-random-content.php

Random Content [random_content] Shortcode

The Random Content shortcode allows users to display random posts on their WordPress site. It fetches posts based on the ‘group_id’ and ‘num_posts’ attributes. If a ‘group_id’ is specified, it filters posts from that group. Otherwise, it fetches random posts from all entries. The ‘num_posts’ attribute determines the number of posts displayed.

Shortcode: [random_content]

Parameters

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

  • group_id – Identifier for the specific group of posts
  • num_posts – Number of posts to display from the group

Examples and Usage

Basic example – Displaying a random post from a specific group. The ‘group_id’ parameter is used to specify the group from which the post will be fetched.

[random_content group_id=3 /]

Advanced examples

Displaying a random post without specifying a group. This will fetch a random post from all available groups. The ‘num_posts’ parameter can be used to specify the number of random posts to display.

[random_content num_posts=2 /]

Combining both ‘group_id’ and ‘num_posts’ parameters. This will fetch a specified number of random posts from a specific group.

[random_content group_id=3 num_posts=2 /]

PHP Function Code

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

Shortcode line:

add_shortcode('random_content', array(&$this, 'new_shortcode'));

Shortcode PHP function:

function new_shortcode($atts)
	{

		$a = shortcode_atts(array(
			'group_id' => '',
			'num_posts' => 1,
		), $atts);

		$content = "";

		// if $group_id is set, then filter results by $group_id
		if (!empty($a['group_id'])) {

			$my_query = new WP_Query(array(
				'post_type' => 'endo_wrc_cpt',
				'posts_per_page' => $a['num_posts'],
				'orderby' => 'rand',
				'tax_query' => array(
					array(
						'taxonomy' => 'endo_wrc_group',
						'field' => 'id',
						'terms' => $a['group_id']
					)
				)
			));
		} else {

			// filter through all entries
			$my_query = new WP_Query(array(
				'post_type' => 'endo_wrc_cpt',
				'posts_per_page' => $a['num_posts'],
				'orderby' => 'rand'
			));
		}

		if ($my_query->have_posts()) {

			while ($my_query->have_posts()) : $my_query->the_post();

				$content .= apply_filters('the_content', get_the_content());

			endwhile;
		} else {
			$content .= __('No posts found.', 'random-content');
		}

		wp_reset_postdata();

		return apply_filters('rc_content', $content);
	}

Code file location:

random-content/random-content/class-random-content.php

Conclusion

Now that you’ve learned how to embed the Random Content Plugin shortcodes, 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 *