Shortcodes Generator

Overview

Use this tool to create custom code for Shortcodes with add_shortcode() function.

Usage

  • Fill in the user-friendly form.
  • Click the “Update Code” button.
  • Copy the code to your project.
  • Or save it as a snippet and share with the community.
Shortcode tag in the content.
e.g. [tag]
The function used in the code.
Self-closing shortcode: [tag]
Enclosing shortcode: [tag]content[/tag]
Enable attributes such as
[tag foo="123" bar="456"].
Use "shortcode_atts_{$shortcode}" filter, to allow shortcode attributes filtering.
Set custom filter name.
Attribute name. Lowercase.
Default value.
e.g. [tag attr_name="default_value"]
Attribute name. Lowercase.
Default value.
Attribute name. Lowercase.
Default value.
Custom code to generate the output.
Should only "return" the text, never produce the output directly.
  Save Snippet
// Add Shortcode
function get_yoast_primary_cat() {

	Fill in your custom taxonomy here
	$yourTaxonomy = 'category';
	
	// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
	$category = get_the_terms( $postId, $yourTaxonomy );
	$useCatLink = true;
	
	// If post has a category assigned.
	if ($category){
		$category_display = '';
		$category_link = '';
		if ( class_exists('WPSEO_Primary_Term') )
		{
		
			// Show the post's 'Primary' category, if this Yoast feature is available, & one is set
			$wpseo_primary_term = new WPSEO_Primary_Term( 'event_cat', get_the_id() );
			$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
			$term = get_term( $wpseo_primary_term );
			if (is_wp_error($term)) { 
			
				// Default to first category (not Yoast) if an error is returned
				$category_display = $category[0]->name;
				$category_link = get_bloginfo('url') . '/' . 'event-category/' . $term->slug;
			} else { 
			
				// Yoast Primary category
				$category_display = $term->name;
				$category_link = get_term_link( $term->term_id );
			}
		} 
		else {
		
			// Default, display the first category in WP's list of assigned categories
			$category_display = $category[0]->name;
			$category_link = get_term_link( $category[0]->term_id );
		}
	
		// Display category
		if ( !empty($category_display) ){
		    if ( $useCatLink == true && !empty($category_link) ){
			echo '<span class="post-category">';
			echo '<a href="'.$category_link.'">'.$category_display.'</a>';
			echo '</span>';
		    } else {
			echo '<span class="post-category">'.$category_display.'</span>';
		    }
		}
		
	}

}
add_shortcode( 'yoast-primary-category', 'get_yoast_primary_cat' );