Skip to content

Instantly share code, notes, and snippets.

@waviaei
Created February 20, 2019 08:06
Show Gist options
  • Select an option

  • Save waviaei/a89b1e7992b195914bb5364c04a7269b to your computer and use it in GitHub Desktop.

Select an option

Save waviaei/a89b1e7992b195914bb5364c04a7269b to your computer and use it in GitHub Desktop.
Faster way to add comma, then delete the last one
// Categories
$start = microtime(true);
$cats = get_the_category();
if ( $cats ) {
foreach ( $cats as $cat ) {
$cat_list[] = '<a href="' . get_category_link( $cat->term_id ) . '">' . $cat->cat_name . '</a>';
}
}
/**
* Filters the list of category links to be displyaed on Post's entry meta
*
* @see tujtheme_post_entry_meta()
*
* @param string $array Category links.
*/
$cat_list = apply_filters( 'tujtheme_filter_post_entry_meta_cat', $cat_list );
$cat_list = implode( ', ', $cat_list );
$cat_list = rtrim( $cat_list, ',' );
echo microtime(true) - $start, PHP_EOL;
0.0013430118560791
// Categories
$start = microtime(true);
$cats = get_the_category();
if ( $cats ) {
$i = 1;
$sep = ', ';
foreach ( $cats as $cat ) {
if ( $i === count( $cats ) ) {
$sep = '';
}
$cat_list[] = '<a href="' . get_category_link( $cat->term_id ) . '">' . $cat->cat_name . '</a>' . $sep;
$i++;
}
}
/**
* Filters the list of category links to be displyaed on Post's entry meta
*
* @see tujtheme_post_entry_meta()
*
* @param string $array Category links.
*/
$cat_list = apply_filters( 'tujtheme_filter_post_entry_meta_cat', $cat_list );
$cat_list = implode( '', $cat_list );
echo microtime(true) - $start, PHP_EOL;
0.00087213516235352
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment