Last active
June 4, 2018 09:56
-
-
Save damiencarbery/ac67e6e2241b4782fc99aab658d9b2fc to your computer and use it in GitHub Desktop.
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
WP_Term::__set_state(array( | |
'term_id' => 24, | |
'name' => 'Foo Parent', | |
'slug' => 'foo-parent', | |
'term_group' => 0, | |
'term_taxonomy_id' => 24, | |
'taxonomy' => 'category', | |
'description' => '', | |
'parent' => 0, | |
'count' => 1, | |
'filter' => 'raw', | |
'cat_ID' => 24, | |
'category_count' => 1, | |
'category_description' => '', | |
'cat_name' => 'Foo Parent', | |
'category_nicename' => 'foo-parent', | |
'category_parent' => 0, | |
)), |
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
<?php | |
// Experiment with get_the_category() to see the information it returns. | |
add_filter( 'the_content', 'append_get_the_category' ); | |
function append_get_the_category( $content ) { | |
if ( !is_single() ) return $content; | |
$post_categories = get_the_category(); | |
return $content . '<pre>' . var_export( $post_categories, true ) . '</pre>'; | |
} |
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
<?php | |
/* | |
Plugin Name: Add CSS classes to post categories list | |
Plugin URI: https://www.facebook.com/groups/genesiswp/permalink/1907687709282417/ | |
Description: Use get_the_category() instead of get_the_category_list() to add CSS classes to each category. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
// Change post meta shortcodes to a custom shortcode. | |
// Maybe I should replace the 'post_categories' shortcode and with my code. | |
add_filter( 'genesis_post_meta', 'dcwd_change_post_meta_shortcode' ); | |
function dcwd_change_post_meta_shortcode( $post_meta ) { | |
return '[post_categories_with_classes] [post_tags]'; | |
} | |
// Return the list of post categories with CSS classes for styling. | |
add_shortcode( 'post_categories_with_classes', 'dcwd_get_the_category_with_classes' ); | |
function dcwd_get_the_category_with_classes( $atts ) { | |
if ( ! is_object_in_taxonomy( get_post_type(), 'category' ) ) { | |
return ''; | |
} | |
$defaults = array( | |
'sep' => ', ', | |
'before' => __( 'Filed Under: ', 'genesis' ), | |
'after' => '', | |
); | |
$atts = shortcode_atts( $defaults, $atts, 'post_categories' ); | |
$post_categories = get_the_category(); | |
// Do nothing if there are no categories. | |
if ( ! $post_categories ) { | |
return ''; | |
} | |
$categories = array(); | |
foreach ( $post_categories as $category ) { | |
$cat_classes = array(); | |
$cat_classes[] = 'cat'; | |
$cat_classes[] = 'cat-' . $category->slug; | |
if ( 0 == $category->parent ) { | |
$cat_classes[] = 'cat-parent'; | |
} | |
else { | |
$cat_classes[] = 'cat-child'; | |
} | |
// TODO: Consider filtering this. | |
$categories[] = sprintf( '<a href="%s" class="%s">%s</a>', esc_url( get_category_link( $category->term_id ) ), implode( ' ', $cat_classes ), $category->name ); | |
} | |
if ( genesis_html5() ) { | |
$output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . implode( ' ', $categories ) . $atts['after'] . '</span>'; | |
} else { | |
$output = '<span class="categories">' . $atts['before'] . implode( ' ', $categories ) . $atts['after'] . '</span>'; | |
} | |
return '<style>.entry-categories .cat-parent { text-transform: uppercase; } .entry-categories .cat-child { font-style: italic; }</style>' . $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment