试试这个。
// **** Remove unwanted classes
function remove_classes($classes, $class, $post_id)
{
// Array that holds the undesired classes
$removeClasses = array(
\'category-\',
\'tag-\'
);
// Array to store the new class names
$newClasses = array();
foreach ($classes as $_class)
{
// Iterate through the array of undesired classes and
// check if the current $_class name starts with the
// undesired class name
$hasClass = FALSE;
foreach ($removeClasses as $_removeClass)
{
if (strpos($_class, $_removeClass) === 0)
{
$hasClass = TRUE;
break;
}
}
// If $_class does not contain an undesired class name,
// add it to the array of new class names.
if (!$hasClass)
{
$newClasses[] = $_class;
}
}
// Return the array of new class names
return ($newClasses);
}
add_filter(\'post_class\', \'remove_classes\', 10, 3);
此筛选器声明不需要的类名数组(
$removeClasses
). 用您不想要的类名展开它。
然后,函数遍历传递的类数组($classes
) 并检查它是否包含您在中定义的类$removeClasses
大堆如果没有,则会将其添加到新数组中($newClasses
). 如果是,它将跳过它。
最后返回新数组$newClasses
.
基本上,它会整理出你不想要的课程。而不是操纵传递的$classes
数组,它创建一个只包含好类的新类,并返回该类。
不过我还没有测试过。我现在没有WordPress安装程序可供摆弄。可能是它不起作用,因为post_class
可能不是正确的筛选器。