Related Posts by Taxonomy Shortcode

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

Before starting, here is an overview of the Related Posts by Taxonomy Plugin and the shortcodes it provides:

Plugin Icon
Related Posts by Taxonomy

"Related Posts by Taxonomy is a dynamic WordPress plugin that enriches your site's user experience. It smartly links related content, keeping visitors engaged with relevant posts based on taxonomy."

★★★★☆ (75) Active Installs: 20000+ Tested with: 5.5.13 PHP Version: false
Included Shortcodes:
  • [related_posts_by_tax]

Related Posts by Taxonomy [related_posts_by_tax] Shortcode

The Related Posts By Taxonomy shortcode is used to display related posts based on their taxonomy. It validates and filters shortcode arguments to customize the output.

Shortcode: [related_posts_by_tax]

Examples and Usage

Basic example – To display related posts by taxonomy using the default settings, use the following shortcode:

[related_posts_by_tax /]

Advanced examples

Displaying related posts by taxonomy with a specified post type and taxonomy. This example will show related posts that are of the ‘product’ post type and belong to the ‘category’ taxonomy:

[related_posts_by_tax post_types="product" taxonomies="category" /]

Displaying related posts by taxonomy with a specified format. This example will show related posts in a list format:

[related_posts_by_tax format="list" /]

Displaying related posts by taxonomy with a specified limit. This example will show only the top 5 related posts:

[related_posts_by_tax posts_per_page="5" /]

Combining multiple parameters. This example will show the top 5 related ‘product’ posts in a list format:

[related_posts_by_tax post_types="product" format="list" posts_per_page="5" /]

These examples demonstrate the flexibility of the related posts by taxonomy shortcode, allowing you to tailor the shortcode to fit your specific needs.

PHP Function Code

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

Shortcode line:

add_shortcode( 'related_posts_by_tax', 'km_rpbt_related_posts_by_taxonomy_shortcode' );

Shortcode PHP function:

function km_rpbt_related_posts_by_taxonomy_shortcode( $args ) {
	// Empty string is returned if no args were added in the shortcode
	$args = is_array( $args ) ? $args : array();

	$settings = km_rpbt_get_default_settings( 'shortcode' );

	/**
	 * Filter default shortcode attributes.
	 *
	 * @since 0.2.1
	 *
	 * @param array $defaults Default feature arguments. See km_rpbt_related_posts_by_taxonomy_shortcode() for
	 *                        for more information about default feature arguments.
	 */
	$defaults = apply_filters( "related_posts_by_taxonomy_shortcode_defaults", $settings );
	$defaults = array_merge( $settings, (array) $defaults );

	// Filter hook shortcode_atts_related_posts_by_tax.
	$args = shortcode_atts( $defaults, $args, 'related_posts_by_tax' );

	$args['type'] = 'shortcode';
	$args = km_rpbt_validate_shortcode_atts( $args );

	/**
	 * Filter validated shortcode arguments.
	 *
	 * @since 0.1
	 *
	 * @param array $args Shortcode arguments. See km_rpbt_related_posts_by_taxonomy_shortcode() for
	 *                    for more information about feature arguments.
	 */
	$args = apply_filters( "related_posts_by_taxonomy_shortcode_atts", $args );
	$args = array_merge( $defaults, (array) $args );

	$args['type'] = 'shortcode';

	return km_rpbt_get_feature_html( 'shortcode', $args );
}

Code file location:

related-posts-by-taxonomy/related-posts-by-taxonomy/includes/class-plugin.php

Conclusion

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