最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - post_class remove tag- or category- from slug

programmeradmin1浏览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

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

Share Improve this question asked Jan 4, 2013 at 16:21 ChuckChuck 5231 gold badge9 silver badges24 bronze badges 4
  • can I ask why you'd like this? – Tom J Nowell Commented 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 Commented Jan 4, 2013 at 18:54
  • would it not make more sense to jsut say data-filter=category-".$category->slug ? – Tom J Nowell Commented Jan 4, 2013 at 20:10
  • that's exactly what I mentioned doing in the comments to toscho's answer – Chuck Commented Jan 4, 2013 at 21:25
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论