如何handle/manage自定义图片?

How to handle/manage custom images?

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

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

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

我需要一些建议。谢谢!

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

First, we set up the global Post variable ($post) so we can have access to the relevant data about our post.

Second, we set up an array of arguments ($args) that define the kind of information we want to retrieve. Specifically, we need to get images that are attached to the current post. We're also going to get all of them, and return them in the same order they appear in the WordPress gallery.

第 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) {

Here we are using the WordPress get_posts function to retrieve the images that match our criteria as defined in $args. We are then storing the results in a variable called $attachments.

Next, we check to see if $attachments exists. If this variable is empty (as will be the case when your post or page has no images attached to it), then no further code will execute. If $attachments does have content, then we move on to the next step.

Set parameters for a WordPress function called wp_get_attachment_image for the images information.

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