8

我正在为客户开发一个特殊的插件。

简而言之:
插件包含 .zip 文件的自动导入。此文件中包含一个 .xml 文件和图像。该插件读取 .xml 文件并将信息插入数据库。

我的问题:
如何以最佳方式处理图像。我应该将它们导入 wordpress 库还是应该自己管理它们。有没有办法使用wordpress画廊,因为它会自动生成缩略图,还是不是一个好主意?

我需要一些建议。谢谢!

4

1 回答 1

4

您应该在 wordpress 图库中添加图像。然后你必须从 wordpress 库中获取这些上传的图片:

步骤 1:准备查询

global $post;

$args = array(
    'post_parent'    => $post->ID,           // For the current post
    'post_type'      => 'attachment',        // Get all post attachments
    'post_mime_type' => 'image',             // Only grab images
    'order'          => 'ASC',               // List in ascending order
    'orderby'        => 'menu_order',        // List them in their menu order
    'numberposts'    => -1,                  // Show all attachments
    'post_status'    => null,                // For any post status
);

首先,我们设置全局 Post 变量($post),以便我们可以访问有关我们的帖子的相关数据。

其次,我们设置了一个参数数组,这些参数($args)定义了我们想要检索的信息类型。具体来说,我们需要获取附加到当前帖子的图像。我们还将获取所有这些,并按照它们在 WordPress 库中出现的相同顺序返回它们。

第 2 步:从 Wordpress 库中检索图像

// Retrieve the items that match our query; in this case, images attached to the current post.
$attachments = get_posts($args);

// If any images are attached to the current post, do the following:
if ($attachments) { 

    // Initialize a counter so we can keep track of which image we are on.
    $count = 0;

    // Now we loop through all of the images that we found 
    foreach ($attachments as $attachment) {

在这里,我们使用 WordPress get_posts函数来检索符合我们定义的标准的图像$args。然后,我们将结果存储在一个名为 的变量中$attachments

接下来,我们检查是否$attachments存在。如果此变量为空(当您的帖子或页面没有附加图像时就是这种情况),则不会执行更多代码。如果$attachments确实有内容,那么我们继续下一步。

为调用wp_get_attachment_image图像信息的 WordPress 函数设置参数。

来源:阅读完整教程或其他步骤的链接 > https://code.tutsplus.com/tutorials/how-to-create-an-instant-image-gallery-plugin-for-wordpress--wp-25321

于 2018-05-28T17:55:56.903 回答