Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active December 23, 2016 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save srikat/86d443921afd8ca26260 to your computer and use it in GitHub Desktop.
Save srikat/86d443921afd8ca26260 to your computer and use it in GitHub Desktop.
Custom Shortcode to display First Category in Genesis. https://sridharkatakam.com/custom-shortcode-to-display-first-category-in-genesis/
add_shortcode( 'post_category', 'sk_post_category_shortcode' );
/**
* Produces the first category link.
*
* Supported shortcode attributes are:
* after (output after link, default is empty string),
* before (output before link, default is 'Tagged With: '),
* sep (separator string between tags, default is ', ').
*
* Output passes through 'genesis_post_categories_shortcode' filter before returning.
*
* @since 1.1.0
*
* @param array|string $atts Shortcode attributes. Empty string if no attributes.
* @return string Shortcode output
*/
function sk_post_category_shortcode( $atts ) {
$defaults = array(
'sep' => ', ',
'before' => __( 'Filed Under: ', 'genesis' ),
'after' => '',
);
$atts = shortcode_atts( $defaults, $atts, 'post_categories' );
$category = get_the_category();
$cat = '<a href="' . get_category_link( $category[0]->term_id ) . '">' . $category[0]->cat_name . '</a>';
if( $category[0] ) {
$output = sprintf( '<span %s>', genesis_attr( 'entry-category' ) ) . $atts['before'] . $cat . $atts['after'] . '</span>';
}
return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );
}
add_shortcode( 'post_category', 'sk_post_category_shortcode' );
/**
* Produces the first category link.
*
* Supported shortcode attributes are:
* after (output after link, default is empty string),
* before (output before link, default is empty string),
* sep (separator string between tags, default is ', ').
*
* Output passes through 'genesis_post_categories_shortcode' filter before returning.
*
* @since 1.1.0
*
* @param array|string $atts Shortcode attributes. Empty string if no attributes.
* @return string Shortcode output
*/
function sk_post_category_shortcode( $atts ) {
$defaults = array(
'sep' => ', ',
'before' => '',
'after' => '',
);
$category = get_the_category();
$cat = '<a href="' . get_category_link( $category[0]->term_id ) . '">' . $category[0]->cat_name . '</a>';
if( $category[0] ) {
$output = '<span class="'. $category[0]->slug .'">' . $atts['before'] . $cat . $atts['after'] . '</span>';
}
return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment