我需要能够:1。)为图像分配类别,以及2.)为所有与其指定类别相对应的图像添加类。
我能够使用以下内容为图像添加类别:
function ug_add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'ug_add_categories_to_attachments' );
我发现了一个向图像添加类的函数:
function add_image_class($class){
$class .= ' additional-class';
return $class;
}
add_filter('get_image_tag_class','add_image_class');
但是我用PHP进展得不够先进。我试过这个
function ug_add_image_class($class){
foreach(get_the_category() as $cat) { $class .= ' category-'.$cat->slug; };
return $class;
}
add_filter('get_image_tag_class','ug_add_image_class');
但没有爱...
帮助?想法?请?和快乐星期五!
答案 0 :(得分:1)
非常感谢birgire在引用的网址上给出答案:
/**
* Append the image categories to the current image class.
*
* @see http://wordpress.stackexchange.com/a/156576/26350
*/
add_filter( 'get_image_tag_class',
function( $class, $id, $align, $size )
{
foreach( (array) get_the_category( $id ) as $cat )
{
$class .= ' category-' . $cat->slug;
}
return $class;
}
, 10, 4 );