Skip to content

Instantly share code, notes, and snippets.

@blogjunkie
Forked from srikat/functions.php
Last active December 23, 2016 10:52
Show Gist options
  • Save blogjunkie/a4f6e79fde5940ae5f45e09297d9757f to your computer and use it in GitHub Desktop.
Save blogjunkie/a4f6e79fde5940ae5f45e09297d9757f 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 ) {
$atts = shortcode_atts (
array(
'sep' => ', ',
'before' => __( 'Filed Under: ', 'genesis' ),
'after' => '',
),
$atts
);
$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 ) {
$atts = shortcode_atts (
array(
'sep' => ', ',
'before' => '',
'after' => '',
),
$atts
);
$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