图片集

图片集(galleries)是在WordPress网站上展示你的图片的最好方式。WordPress在媒体上传器中默认捆绑了创建图片集功能,允许你创建一个简单的图库。

注意:在添加图片集之前,你的媒体库中必须有图片。否则,你需要把图片上传到库中,然后才能继续创建图片集。

 

图片集功能允许你使用一个简单的短码在文章和页面上添加一个或多个图片画廊。

图片集短代码的基本形式是:

[[gallery]]

如果您在文章或页面中使用[gallery]短代码而不使用ids参数,那么只有"附在"该文章或页面上的图片才会被显示。

如果你需要添加多个带ID的图片,请使用以下示例短码

[[gallery ids="10, 205, 552, 607"]]
//Note: 10, 205, 552 and 607 are the IDs of respected image.

注意:为图片集找到适当的图像ID。进入媒体库,点击被选中的图像,ID将出现在URL上。

要在模板文件中使用短码,请使用do_shortcode()函数。在你的模板文件中插入以下代码:

echo do_shortcode( [[gallery]] );

如果你需要使用带ID的短码,在你的模板文件中插入以下代码:

echo do_shortcode( [[gallery ids="10, 205, 552, 607"]] );

 

使用方法

有一些选项可以使用以下语法来指定:

[[gallery option1="value1" option2="value2"]]

如果你想直接在模板文件上打印图片集,使用`do_shortcode()`函数,如下所示:

<?php echo do_shortcode('[[gallery option1="value1"]]'); ?>

如果你需要过滤短代码,下面的例子给你一些提示:

// Note: 'the_content' filter is used to filter the content of the
// post after it is retrieved from the database and before it is 
// printed to the screen
 <?php $gallery_shortcode = '[[gallery id="' . intval( $post->post_parent ) . '"]]';
    print apply_filters( 'the_content', $gallery_shortcode );
 ?>

 

支持的选项

图片集代码支持下面列出的基本选项:

 

Orderby

‘orderby’指定了缩略图显示的顺序。默认的顺序是‘menu_order’

  • menu_order: 你可以在 "添加媒体" 弹出窗口的 "图库" 选项卡中重新安排图片的顺序
  • title: 按媒体库中图片的标题排序
  • post_date: 按date/time排序
  • rand: 随机排序
  • ID: 指定的文章ID

 

Order

order 指定用于显示缩略图的排序方式,ASC或DESC。例如,按ID和DESC排序:

[[gallery order="DESC" orderby="ID"]]

如果你需要在模板文件中打印,使用do_shortcode()函数

echo do_shortcode(' [[gallery order="DESC" orderby="ID"]] ');

 

Columns

列(Columns)选项指定了图片集中的列数,默认值是3。
如果你想增加图片集的列数,请使用以下短码。

[[gallery columns="4"]]

在模板文件中打印,使用do_shortcode()函数

<?php echo do_shortcode(' [[gallery columns="4"]] '); ?>

 

IDs

IDs选项加载指定文章ID的图片

如果你想用指定的文章ID来显示所附的图片,请遵循以下代码示例:

// Note: remove each space between brackets and 'gallery' and brackets and `123"`.
//Here "123" stands for the post IDs. If you want to display more than
//one ID, separate the IDs by a comma `,`.
[ gallery id="123" ]

使用‘do_shortcode’函数,在模板文件中打印结果:

// Note: remove each space between brackets and 'gallery' and brackets and `123"`.
<?php echo do_shortcode(' [ gallery id="123" ] '); ?>

 

Size

尺寸(Size)决定了用于显示缩略图的图像大小。有效值包括 "thumbnail"、"medium"、"large"、"full"和任何其他用add_image_size()注册的图像尺寸,默认值是"thumbnail"。"thumbnail"、"medium"和 "large"的图片大小可以在WordPress管理面板的设置>媒体下配置。

例如,显示medium尺寸的图片集:

[[gallery size="medium"]]

一些高级选项也可用于图片集短代码。

 

itemtag

用来封包图片集中每个项目的HTML标签名称,默认是“dl”

 

icontag

用来封包图片集中每个缩略图图标的HTML标签名称,默认是“dt”.

 

captiontag

用来封包每个标题的HTML标签名称,默认是“dd”.

你可以改变默认值:

[[gallery itemtag="div" icontag="span" captiontag="p"]]

 

指定你希望图片链接的位置,默认值是链接到附件的固定链接,选项:

  • file – 直接链接到图像文件
  • none – 没有链接

示例

[[gallery link="file"]]

 

Include

Include(包含)允许你插入一个由逗号分隔的附件ID组成的"数组",只显示这些附件的图片。

[[gallery include="23,39,45"]]

 

Exclude

Exclude(排除)允许你插入一个以逗号分隔的附件ID的"数组",以不显示这些附件的图像。请注意,include和exclude不能同时使用。

[[gallery exclude="21,32,43"]]