gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:20:52 25 4
gpt4 key购买 nike

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

情况简述:
该插件包含 .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
);

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.

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

关于wordpress - 如何处理/管理自定义图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50503782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com