-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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