gpt4 book ai didi

php - Wordpress: get_attached_media ('image' ) 按标题排序

转载 作者:可可西里 更新时间:2023-11-01 00:31:34 24 4
gpt4 key购买 nike

我想获取特定帖子的所有图片。这适用于:

$media = get_attached_media('image');

我现在需要的是按标题对这些图像进行排序。我已经可以生成数组中标题的列表:

for ($i = 0; $i < count($media); $i++) {
get_the_title(array_keys($media)[$i])
}

我不知道如何按标题排序。谁能帮忙?

最佳答案

获取已经订购的附件而不是订购结果数组会更好,对吧?这将为您节省代码、麻烦和处理。

如果您查看 WP Codex,get_attached_media()电话 get_children() ,它调用 get_posts() (是的,迅速升级)。在 WordPress 中,附件(以及几乎任何东西)本质上是一个 post

考虑到所有这些,这应该会为您获取按标题排序的帖子所附图片列表:

$media = get_posts(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'title',
'order' => 'ASC'
));

编辑: 正如 ViszinisA 指出的那样和 Pieter Goosen ,我直接将调用更改为 get_posts()。调用 get_children() 毫无意义。

注意:'post_parent' 参数是必需的,所以我使用 get_the_ID() 添加它作为它的值。请记住,您需要在循环中 get_the_ID() 才能检索当前帖子 ID。在循环外使用时,应根据上下文更改此参数值。

关于php - Wordpress: get_attached_media ('image' ) 按标题排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943478/

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