內容簡介
這個外掛有 3 個主要的類別,每個類別都有一個簡碼。
簡碼:
* [image src=”%url%”]
* [theme-image src=”%url%”]
* [upload-image src=”%url%”]
* [picture src=”%baseurl%” ext=”%ext%” sizes=”%sizes%”]
* [theme-picture src=”%baseurl%” ext=”%ext%” sizes=”%sizes%”]
* [upload-picture src=”%baseurl%” ext=”%ext%” sizes=”%sizes%”]
* [thumbnail]
類別:
* WaughJ\HTMLImage\HTMLImage
* WaughJ\WPThemeImage\WPThemeImage
* WaughJ\WPUploadImage\WPUploadImage
* WaughJ\HTMLPicture\HTMLPicture
* WaughJ\WPThemePicture\WPThemePicture
* WaughJ\WPUploadPicture\WPUploadPicture
* WaughJ\WPPostThumbnail\WPPostThumbnail
一般的圖像簡碼會載入指定的源 URL,不會包含破壞快取的版本參數。這主要是供其他類別使用,如下面兩個。
一般的圖片簡碼會載入%base%.%ext%,以及根據所給尺寸載入額外的源。請閱讀 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture 以深入了解 picture 元素的工作方式。
主題圖像和圖片簡碼會從當前主題目錄載入圖像。
上傳圖像和圖片簡碼會從上傳目錄載入圖像。
縮略圖簡碼會載入文章的縮略圖。如果給出 post-id 屬性,則會載入該 ID 的縮略圖。如果未給出 ID,則會嘗試提供當前文章的 ID(get_the_ID() 給出的)。可以通過 %tag-name%-%attribute-name% 提供 img、source 和 picture 屬性。例如,要設置圖像的類,加入 img-class=”%class%”。
HTML 和主題類別具有近乎相同的介面:必須的源和可選的引數。對於簡碼來說,這意味著一個“src”屬性以及任何其他有效的 HTML 屬性;對於類別來說,這意味著一個散列映射作為第二個可選引數。
WPUploadImage 類需要一個必需的 ID 整數,表示媒體部分中的圖像 ID。第二個引數是可選的尺寸字符串,表示在 WordPress 中註冊的尺寸類型的縮略名,使用“responsive”自動縮放圖像以使用 srcset 動態載入不同窗口大小的尺寸,可選的第三個引數是額外的屬性散列映射,如上所述。
WPUploadPicture 需要一個必需的 ID 整數和可選屬性散列映射。
除了任何有效的 HTML 屬性外,WPThemeImage 和 WPThemePicture 類還接受“directory”屬性,並且它們和上傳類別接受“show-version”屬性。如果設置了前者,則會自動將源放在給定目錄中。如果將“show-version”屬性設置為 false,則不會嘗試找到圖像的最後修改類型為其提供版本參數以破壞快取損壞。
如果未設置“alt”屬性,則會自動添加一個空的一個以確保通過它們製作的所有圖像都具有 alt tag。
為了讓與主題圖像對象一起工作最小化不便,這些圖像都在不是頂級目錄的同一目錄中,您可以在 WordPress 管理員中全局設置共享內部目錄,方法是“外觀”->“主題”->“目錄”,或直接在 WPThemeImage 本身的類上使用靜態 setDefaultSharedDirectory 方法直接設置。之後,所有初始化的 WPThem
外掛標籤
開發者團隊
原文外掛簡介
This plugin has 3 main classes, with a shortcode for each 1.
Shorcodes:
* [image src=”%url%”]
* [theme-image src=”%url%”]
* [upload-image src=”%url%”]
* [picture src=”%baseurl%” ext=”%ext%” sizes=”%sizes%”]
* [theme-picture src=”%baseurl%” ext=”%ext%” sizes=”%sizes%”]
* [upload-picture src=”%baseurl%” ext=”%ext%” sizes=”%sizes%”]
* [thumbnail]
Classes:
* WaughJ\HTMLImage\HTMLImage
* WaughJ\WPThemeImage\WPThemeImage
* WaughJ\WPUploadImage\WPUploadImage
* WaughJ\HTMLPicture\HTMLPicture
* WaughJ\WPThemePicture\WPThemePicture
* WaughJ\WPUploadPicture\WPUploadPicture
* WaughJ\WPPostThumbnail\WPPostThumbnail
The regular image shortcode loads the exact source URL given, without a cache-corruption-breaking version parameter. It is mainly for use by other classes, like the next 2.
The regular picture shortcode loads %base%.%ext%, as well as extra sources based on the sizes given. Read https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture for mo’ info on how the picture element works.
The theme image & picture shortcodes load images from the current theme directory.
The upload image & picture shortcodes load images from the uploads directory.
The thumbnail shortcode loads a post’s thumbnail. If the post-id attribute is given, it loads the thumbnail o’ that ID. If no ID is given, it tries to supply the ID o’ whatever the current post is ( whatever get_the_ID() gives ). Img, source, & picture attributes can be given through %tag-name%-%attribute-name%. For example, to set the image’s class, add img-class=”%class%”.
The HTML & Theme classes have near the same interface: a mandatory source & optional arguments. For the shortcodes, this means a “src” attribute as well as any other valid HTML attributes; for the classes, it means a hash map as an optional 2nd argument.
The WPUploadImage class takes a mandatory ID integer representing the ID o’ the image in the media section o’ the WordPress admin, the 2nd argument is an optional size string representing the slug o’ the size type as registered in WordPress, with “responsive” for automatically an image that uses srcset to dynamically load the size for different window sizes, & the optional 3rd argument is a hash map for extra attributes, as ‘bove.
The WPUploadPicture takes a mandatory ID integer & the optional attributes hash map.
In addition to any valid HTML attributes, the WPThemeImage & WPThemePicture classes also accept “directory” & they & the uploads classes accept the “show-version” attributes. The former, if set, will automatically put the source in the given directory; the “show-version” attribute, if set to false, won’t try to find the image’s last modified type to give it a version parameter for breaking cache corruption.
If “alt” attribute is not set, an empty 1 will automatically be added to the HTML generated, ensuring that all images made through these will have an alt tag.
To make working with theme image objects with minimal inconvenience for images that are all in the same directory that is not the topmost directory o’ the theme directory, you can globally set the inner shared directory in the WordPress admin through Appearances -> Theme -> Directories, or directly in PHP with WPThemeImage’s static setDefaultSharedDirectory method on the class itself. After that, all initialized WPThemeImage & WPThemePicture instances, including the shortcodes, will automatically use that shared directory if a different 1 isn’t provided.
To add HTML attributes to WPThemePicture & WPUploadPicture shortcodes, prefix them with “img-“, “picture-“, or “source-” depending on what tag you want the attribute given to. For example, to apply a class to the img tag, give the shortcode the attribute “img-class”.
Examples
use WaughJ\WPUploadImage\WPUploadImage;
echo new WPUploadImage
(
31,
[
'class' => 'center-img portrait',
'alt' => 'King'
]
);
&
[upload-image media-id="31" class="center-img portrait" alt="King"]
Will generate something like 
use WaughJ\WPThemeImage\WPThemeImage;
WPThemeImage::setDefaultSharedDirectory( 'img' );
echo new WPThemeImage( 'photo.jpg' );
Will generate something like 
[upload-picture media-id="8"]
Will generate something like 
