我需要的风格,四周嵌在没有字幕后的图像去的元素,但据我可以告诉有没有办法自动添加一个类,或目标s的一个
内只有不使用jQuery或一些东西.
这是他们默认出来的方式:
是否有一个简单的wordpress PHP方法,我可以在默认情况下在所有这些元素上设置一个简单的".img"类?
令人困惑的是,这不是Wordpress中的标准功能,很多人抱怨它,但据我所知,没有实际的解决方案.
为了澄清,这应该适用于帖子中的现有图像,而不仅仅是我未来的帖子!
如果您能够编辑functions.php文件,则添加此代码.经过测试和验证:
/** * Attach a class to linked images' parent anchors * Works for existing content */ function give_linked_images_class($content) { $classes = 'img'; // separate classes by spaces - 'img image-link' // check if there are already a class property assigned to the anchor if ( preg_match('/<a.*? class=".*?"><img/', $content) ) { // If there is, simply add the class $content = preg_replace('/(<a.*? class=".*?)(".*?><img)/', '$1 ' . $classes . '$2', $content); } else { // If there is not an existing class, create a class property $content = preg_replace('/(<a.*?)><img/', '$1 class="' . $classes . '" ><img', $content); } return $content; } add_filter('the_content','give_linked_images_class');