wordpress图像大小_如何更改WordPress帖子中的图像属性

wordpress图像大小

WordPress allows users to place an image into page or post content. Typically, the following HTML code is produced:

WordPress允许用户将图像放入页面或发布内容。 通常,将生成以下HTML代码:

<img src="http://www.mysite.com/wp-content/uploads/2011/07/myimage.jpg" 
alt="my image" title="my image title" 
width="100" height="100" class="aligncenter size-full wp-image-123" />

Nothing too horrific, but what if you want to:

没什么太可怕的,但是如果您想:

  • add your own classes

    添加自己的课程
  • remove the domain from the URL (probably unnecessary unless you’re using RSS feeds)

    从URL中删除域(除非您使用RSS feed,否则可能不必要)
  • remove the width and height attributes for scaled images in a responsive design?

    在响应式设计中删除缩放图像的宽度和高度属性?

Fortunately, WordPress provides a couple of filter functions to help you modify the HTML. They can be placed in your theme’s functions.php file or within a plug-in.

幸运的是,WordPress提供了一些过滤器功能来帮助您修改HTML。 它们可以放置在主题的functions.php文件中或插件中

important: <img> code is created on insert 重要: <img>代码是在插入时创建的

Unlike other WordPress content filters, <img> tags are generated when the editor inserts the image into the post. Therefore, these functions will not change code for images which have already been added to a post.

与其他WordPress内容过滤器不同,<img>标签是在编辑器将图像插入帖子时生成的。 因此,这些功能将不会更改已添加到帖子中的图像的代码。

Therefore, the only way to test your code is to repeatedly add and remove images. I recommend switching to WordPress’s HTML view to check the result.

因此,测试代码的唯一方法是重复添加和删除图像。 我建议切换到WordPressHTML视图以检查结果。

更改图像类别 (Changing the Image Class)

The easiest and most commonly used filter is for the image class:

最简单,最常用的过滤器是图像类:

function image_tag_class($class, $id, $align, $size) {
	return $align;
}
add_filter('get_image_tag_class', 'image_tag_class', 0, 4);

The image_tag_class() function accepts 4 string parameters: the existing class, the image ID (numeric), the alignment (none, left, right, center), and the size (normally ‘full’). It returns a string which will become the <img> tag class. In this example, we’re simply returning the alignment string.

image_tag_class()函数接受4个字符串参数:现有类,图像ID(数字),对齐方式(无,左,右,居中)和大小(通常为“完整”)。 它返回一个字符串,该字符串将成为<img>标记类。 在此示例中,我们仅返回对齐字符串。

先进的图像修改 (Advanced Image Modification)

If we need to make fundamental changes to the <img> tag, we need a filter which can modify the returned HTML directly using string or regular expression replacements, e.g.

如果需要对<img>标签进行根本性的更改,则需要一个过滤器,该过滤器可以使用字符串或正则表达式替换直接修改返回HTML,例如

function image_tag($html, $id, $alt, $title) {
	return preg_replace(array(
			'/'.str_replace('//','//',get_bloginfo('url')).'/i',
			'/s+width="d+"/i',
			'/s+height="d+"/i',
			'/alt=""/i'
		),
		array(
			'',
			'',
			'',
			'alt="' . $title . '"'
		),
		$html);
}
add_filter('get_image_tag', 'image_tag', 0, 4);

The image_tag() function accepts 4 string parameters: the generated HTML, the image ID (numeric), the alt and title text. In this example, we’ve stripped the domain name from the src URL, removed the height and width attributes and replaced empty alt attributes with the title text. You can change the function accordingly for your own substitutions.

image_tag()函数接受4个字符串参数:生成HTML,图像ID(数字),alt和标题文本。 在此示例中,我们从src URL中删除了域名,删除了height和width属性,并将空的alt属性替换为标题文本。 您可以相应地为自己的替换功能。

While HTML <img> tag modification is not something you’ll need every day, it’s useful to know that WordPress supports the feature.

尽管HTML <img>标记修改并不是您每天都需要的,但了解WordPress支持该功能很有用。

翻译自: https://www.sitepoint.com/wordpress-change-img-tag-html/

wordpress图像大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值