如何处理/管理自定义图像?

yfa*_*ain 8 wordpress wordpress-plugin-creation

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

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

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

我需要一些建议.谢谢!

小智 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
);
Run Code Online (Sandbox Code Playgroud)

首先,我们设置全局 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) {
Run Code Online (Sandbox Code Playgroud)

在这里,我们使用 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