-
Notifications
You must be signed in to change notification settings - Fork 29
/
taxonomy-post-class.php
83 lines (71 loc) · 2.24 KB
/
taxonomy-post-class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Plugin Name: Taxonomy Post Class
* Plugin URI: http://wordpress.stackexchange.com/q/66408/6035
* Description: Add a taxonomy slug to a posts class.
* Version: 1.0
* Text Domain: wpse
* Author: Christopher Davis
* Author URI: http://christopherdavis.me
* License: GPL-2.0+
*
* Copyright 2012 Christopher Davis <http://christopherdavis.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category WordPress
* @author Christopher Davis <http://christopherdavis.me>
* @copyright 2012 Christopher Davis
* @license http://opensource.org/licenses/GPL-2.0 GPL-2.0+
*/
!defined('ABSPATH') && exit;
Taxonomy_Post_Class::init();
class Taxonomy_Post_Class
{
/**
* The post type to which you want to add classes. CHANGE THIS.
*
*/
const TYPE = 'post';
/**
* the taxonomy whose slugs you want to add. CHANGE THIS.
*
*/
const TAX = 'category';
private static $ins = null;
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public static function init()
{
add_filter('post_class', array(self::instance(), 'add_class'), 10, 3);
}
public function add_class($classes, $cls, $post_id)
{
if (self::TYPE !== get_post_type($post_id)) {
return $classes;
}
return array_merge($classes, $this->getSlugs($post_id));
}
private function getSlugs($post_id)
{
$terms = get_the_terms($post_id, self::TAX);
if (!$terms || is_wp_error($terms)) {
return array();
}
return wp_list_pluck($terms, 'slug');
}
}