如何在 WordPress 中将图像导入为 HTML 列表?

发布于 2024-10-26 12:48:57 字数 152 浏览 5 评论 0原文

我的问题很简单:是否可以在帖子中一次导入许多图像(例如图库),但例如在

    内? 我认为图库导入是在帖子中导入一组图像的唯一方法,但在这种情况下我无法处理 HTML。 我可能是错的,但我没有找到任何相关信息。感谢您的帮助。

My question is very simple: Is it possible to import many images at once in a post (such as the gallery does) but inside a <ul> for instance ?
The gallery import is, I think, the only way to import a group of images inside a post, but I can't handle the HTML in this case.
I can be wrong, but I didn't find anything about that. Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以歌曲疗慰 2024-11-02 12:48:58

您可以将图像插入图库中,而无需将其导入帖子中。然后,在单个帖子模板中,您可以查询帖子附加的图像以显示它们。

附件只是附加到帖子上的帖子,所以让我们使用 get_posts():

function get_post_images($post) {
    $args = array(
        'post_type' => 'attachment', // Images attached to the post
        'numberposts' => -1, // Get all attachments
        'post_status' => null, // I don’t care about status for gallery images
        'post_parent' => $post->ID, // The parent post
        'exclude' => get_post_thumbnail_id($post->ID), // Exclude the thumbnail if you want it on your article list but not inside an article
        'post_mime_type' => 'image', // The attachment type
        'order' => 'ASC',
        'orderby' => 'menu_order ID', // Order by menu_order then by ID
    );
    return get_posts($args);
}

我建议将此函数放在functions.php 文件中。

在您的 single.php 文件中:

<ul>
<?php
    $images = get_post_images($post);
    foreach ($images as $image):
?>
<li><?php echo wp_get_attachment_image($image->ID, 'medium'); ?></li>
<?php endforeach; ?>
</ul>

WP Codex 链接:

You can insert your images in the gallery without importing it into your post. Then, in the single post template, you can query the images attached to the post to display them.

Attachments are just posts attached to a post, so let’s use get_posts():

function get_post_images($post) {
    $args = array(
        'post_type' => 'attachment', // Images attached to the post
        'numberposts' => -1, // Get all attachments
        'post_status' => null, // I don’t care about status for gallery images
        'post_parent' => $post->ID, // The parent post
        'exclude' => get_post_thumbnail_id($post->ID), // Exclude the thumbnail if you want it on your article list but not inside an article
        'post_mime_type' => 'image', // The attachment type
        'order' => 'ASC',
        'orderby' => 'menu_order ID', // Order by menu_order then by ID
    );
    return get_posts($args);
}

I suggest to place this function in the functions.php file.

In your single.php file:

<ul>
<?php
    $images = get_post_images($post);
    foreach ($images as $image):
?>
<li><?php echo wp_get_attachment_image($image->ID, 'medium'); ?></li>
<?php endforeach; ?>
</ul>

WP Codex links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文