Bnfw Shortcode

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

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

Plugin Icon
Customize WordPress Emails and Alerts – Better Notifications for WP

"Customize WordPress Emails and Alerts – Better Notifications for WP is a unique plugin that allows you to tailor your WordPress notifications and emails for an enhanced user experience."

★★★★☆ (174) Active Installs: 40000+ Tested with: 6.2.3 PHP Version: 7.1
Included Shortcodes:
  • [post_term]

Bnfw [post_term] Shortcode

The BNFW plugin shortcode, ‘post_term’, retrieves and displays the names of terms associated with a specific post. It accepts two parameters: ‘taxonomy’ and ‘id’. The ‘taxonomy’ parameter specifies the type of terms to retrieve, while ‘id’ denotes the post’s ID. The shortcode returns a comma-separated list of term names. In case of an error, it returns an empty string.

Shortcode: [post_term]

Parameters

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

  • taxonomy – specifies the type of taxonomy to retrieve terms from
  • id – the unique identifier of the post to get terms for

Examples and Usage

Basic example – A basic usage of the shortcode to retrieve terms from a specific taxonomy related to a post. In this example, we are retrieving categories (taxonomy) related to the post with the ID of 1.

[post_term taxonomy="category" id="1" /]

Advanced examples

Retrieving tags (taxonomy) related to the post with the ID of 2. If there are multiple tags, they will be returned as a comma-separated list.

[post_term taxonomy="post_tag" id="2" /]

Another advanced example could be retrieving a custom taxonomy. Suppose we have a custom taxonomy named “genre” for a custom post type “book”. We can retrieve the genres related to a book post with the ID of 3 like this:

[post_term taxonomy="genre" id="3" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'post_term', array( $this, 'post_term_shortcode_handler' ) );

Shortcode PHP function:

function post_term_shortcode_handler( $atts ) {
			$atts = shortcode_atts(
				array(
					'taxonomy' => '',
					'id'       => 0,
				),
				$atts
			);

			$terms = wp_get_post_terms( $atts['id'], $atts['taxonomy'], array( 'fields' => 'names' ) );

			if ( ! is_wp_error( $terms ) ) {
				return implode( ', ', $terms );
			}

			return '';
		}

Code file location:

bnfw/bnfw/includes/engine/class-bnfw-engine.php

Conclusion

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