Skip to content

Instantly share code, notes, and snippets.

@birgire
Last active October 13, 2019 04:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birgire/0fedee7b58327106be1d to your computer and use it in GitHub Desktop.
Save birgire/0fedee7b58327106be1d to your computer and use it in GitHub Desktop.
WordPress: Draft idea on how one could simplify the get_the_category_list() to address code duplication - WP 4.4.2
/**
* Retrieve category list in either HTML list or custom format.
*
* @since 1.5.1
*
* @global WP_Rewrite $wp_rewrite
*
* @param string $separator Optional, default is empty string. Separator for between the categories.
* @param string $parents Optional. How to display the parents.
* @param int $post_id Optional. Post ID to retrieve categories.
* @return string
*/
function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
global $wp_rewrite;
if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) {
/** This filter is documented in wp-includes/category-template.php */
return apply_filters( 'the_category', '', $separator, $parents );
}
/**
* Filter the categories before building the category list.
*
* @since 4.4.0
*
* @param array $categories An array of the post's categories.
* @param int|bool $post_id ID of the post we're retrieving categories for. When `false`, we assume the
* current post in the loop.
*/
$categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id );
if ( empty( $categories ) ) {
/** This filter is documented in wp-includes/category-template.php */
return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
}
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
$thelist = '';
// Seperator - Begin
if( '' === $seperator )
$thelist .= '<ul class="post-categories">';
foreach ( $categories as $category ) {
// Seperator - Before
if( '' === $seperator )
$thelist .= "\n\t<li>";
elseif ( ! empty( $thelist ) )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name.'</a>';
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
}
// Seperator - After
if( '' === $seperator )
$thelist .= "\n\t<li>";
}
// Seperator - End
if( '' === $seperator )
$thelist .= '</ul>';
/**
* Filter the category or list of categories.
*
* @since 1.2.0
*
* @param array $thelist List of categories for the current post.
* @param string $separator Separator used between the categories.
* @param string $parents How to display the category parents. Accepts 'multiple',
* 'single', or empty.
*/
return apply_filters( 'the_category', $thelist, $separator, $parents );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment