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



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

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

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

我需要一些建议。谢谢!

您应该在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

最新更新