post_class remove tag- or category- from slug
-
20-04-2021 - |
Question
I've got this in my theme
<article <?php post_class('archiveMain'); ?>>
But for tags and categories it adds tag- or category- before the slug, any way to remove that?
Example: tag-sales would just be sales, category-webinar, just webinar
La solution
You can filter post_class
and change these class names:
add_filter( 'post_class', 'wpse_78237_post_class' );
function wpse_78237_post_class( $classes )
{
$out = array ();
foreach ( $classes as $class )
{
if ( 0 === strpos( $class, 'tag-' ) )
{
$out[] = substr( $class, 4 );
}
elseif ( 0 === strpos( $class, 'category-' ) )
{
$out[] = substr( $class, 9 );
}
else
{
$out[] = $class;
}
}
return array_unique( $out );
}
But be aware this could result in collisions with other class names, in body_class
for example. I would not do that.
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange