Created
November 21, 2016 11:33
-
-
Save jekkilekki/cc81b16f13967ab58ad5ab91a99742c6 to your computer and use it in GitHub Desktop.
WordPress taxonomy dropdowns - if too long (default is to show all taxonomies)
This file contains hidden or 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
jQuery(document).ready(function($) { | |
/* | |
* For Taxonomy dropdowns in Post meta | |
*/ | |
$( '.cat-links .jkl-tax-switch, .tags-links .jkl-tax-switch' ).click( function( e ) { | |
e.preventDefault(); | |
if( $( this ).next( 'ul' ).hasClass( 'childopen' ) ) { | |
$( this ).next( 'ul' ).removeClass( 'childopen' ); | |
} else { | |
$( this ).next( 'ul' ).addClass( 'childopen' ); | |
} | |
}); | |
$( '.cat-links .jkl-tax-switch, .tags-links .jkl-tax-switch' ).hover( function ( e ) { | |
e.preventDefault(); | |
if( $( '.entry-meta .cat-links ul' ).hasClass( 'childopen' ) ) { | |
$( '.entry-meta .cat-links ul' ).removeClass( 'childopen' ); | |
} else { | |
$( '.entry-meta .cat-links ul' ).addClass( 'childopen' ); | |
} | |
}); | |
}); |
This file contains hidden or 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 | |
/** | |
* Better Categories (displays them as a dropdown menu) | |
*/ | |
function jkl_better_taxonomy_listing( $taxonomy_type, $length = -1 ) { | |
if ( 'post' === get_post_type() ) { | |
// If the taxonomy type is 'category' | |
if( 'category' === $taxonomy_type ) { | |
/* translators: used between list items <li> */ | |
$list = get_the_category_list( __( '</li><li>', 'jkl' ) ); | |
$search = '</a>'; | |
$separator = ', '; | |
$test = $list && jkl_categorized_blog(); | |
$class = 'cat-links'; | |
$string = esc_attr__( 'Filed under: ', 'jkl' ); | |
} | |
// If the taxonomy type is 'tag' | |
else if( 'tag' === $taxonomy_type ) { | |
/* translators: used between list items, there is a space after the comma */ | |
$list = get_the_tag_list( '', esc_html__( ', ', 'jkl' ) ); | |
$search = ','; | |
$separator = ''; | |
$test = $list; | |
$class = 'tags-links'; | |
$string = esc_attr__( 'Tagged: ', 'jkl' ); | |
} | |
if ( $test ) { | |
echo '<ul class="' . $class . '">'; | |
echo $string; | |
// So long as the Category list contains more items than our specified length, | |
// we'll display the number of items specified, followed by a dropdown menu | |
// that houses the rest of the Categories | |
// Also note: -1 means we show ALL the categories without a dropdown | |
if( substr_count( $list, $search ) > $length && $length !== -1 ) { | |
// For the number of specified items | |
for( $i = 1; $i <= $length; $i++ ) { | |
// The Category will be the substring from the beginning of the string to the end of the first link </a> tag | |
$item = substr( $list, 0, ( strpos( $list, $search ) + strlen( $search ) ) ); | |
// If the NEXT item is the last item, give it a special CSS class to specify that | |
// just in case we want to style that one individually | |
if( $i == $length ) { | |
echo str_replace( '<a ', '<a class="final-tax-link" ', $item ); | |
} else { | |
// otherwise, separate every Category with a comma | |
echo $item . $separator; | |
} | |
// Now, remove that first category from the list (string) and continue the loop | |
$list = substr( $list, ( strpos( $list, $search ) + strlen( $search ) ) ); | |
} | |
// After looping through our specified number of Categories, output the rest of them in a dropdown menu | |
if( ! empty( $list ) ) { | |
echo '<span class="jkl-tax-switch"><i class="fa fa-angle-down"></i></span>'; | |
printf( '<ul class="submenu dropdown">' . $list . '</ul>', $list ); // WPCS: XSS OK. | |
} | |
} | |
// Else, in the case that our Category list is shorter than the specified $length | |
// OR if we've specified -1 as the $length (to show all values) | |
else { | |
// So long as we have Categories left in the string | |
while( $list !== '' ) { | |
// Do the same as above, find the first Category and get ready to output it | |
$item = substr( $list, 0, ( strpos( $list, $search ) + strlen( $search ) ) ); | |
// If the Category list has only ONE Category (we check for a single </a> tag) | |
if( substr_count( $list, $search ) == 1 ) { | |
// Output it without a comma | |
echo $item; | |
} else { | |
// Otherwise, separate the Categories with commas | |
echo $item . $separator; | |
} | |
// Now, remove that first category from the list (string) and continue the loop | |
$list = substr( $list, ( strpos( $list, $search ) + strlen( $search ) ) ); | |
} | |
} | |
// Close the links list | |
echo '</ul>'; | |
} | |
} // END taxonomy list | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment