0

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

4
  • can I ask why you'd like this?
    – Tom J Nowell
    Jan 4, 2013 at 16:31
  • I'm going to be working with isotope to do some filtering, isotope.metafizzy.co/docs/filtering.html, I've built my links for filtering like this, <a href="#"data-filter=".'.$category->slug.'">'.$category->name.'</a>, I need to match $category->slug to the class but it appeared I was getting the slug with category or tag tacked on at the beginning.
    – Chuck
    Jan 4, 2013 at 18:54
  • would it not make more sense to jsut say data-filter=category-".$category->slug ?
    – Tom J Nowell
    Jan 4, 2013 at 20:10
  • that's exactly what I mentioned doing in the comments to toscho's answer
    – Chuck
    Jan 4, 2013 at 21:25

1 Answer 1

1

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.

3
  • Are you saying you wouldn't use something like this for body_class or that using this for post_class could cause issue with body_class?
    – Chuck
    Jan 4, 2013 at 18:57
  • 1
    @Chuck Both. Just imagine a category sticky or a tag archive. The prefixes don’t hurt.
    – fuxia
    Jan 4, 2013 at 19:00
  • I see now what you are getting at. I guess I'll just add category- or tag- to the slug I'm trying to match to the post_class, and that will work just as well. Thanks for the answer, and for steering me away from using it.
    – Chuck
    Jan 4, 2013 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.