从 WordPress 媒体库获取所有图像以及与其关联的帖子的链接
我正在尝试从 wordpress 媒体库中获取所有图像以及与其关联的帖子的链接。
我正在使用此代码,我获取了所有图像,但链接是图像 URL 的链接,而不是与它们关联的帖子的链接。
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_attachment_link($post->ID, false);
}
}
?>
使用此代码我获得了所有图像,但没有链接。
<?php $wpb_all_query =
new WP_Query(array('post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => -1)); ?>
<?php if ($wpb_all_query->have_posts()) : ?>
<?php while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
<?php echo get_image_tag(get_the_ID(), '', '', 'none', 'medium'); ?>
<?php endwhile; ?>
<?php endif; ?>
有人可以帮我吗?
谢谢
I'm trying to get all the images from the wordpress media library with link to the post they are associated with.
I'm using this code, I get all the images, but the link is the link to the URL of the image, not the post they are associated with.
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_attachment_link($post->ID, false);
}
}
?>
using this code I get all the images, but without the link.
<?php $wpb_all_query =
new WP_Query(array('post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => -1)); ?>
<?php if ($wpb_all_query->have_posts()) : ?>
<?php while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
<?php echo get_image_tag(get_the_ID(), '', '', 'none', 'medium'); ?>
<?php endwhile; ?>
<?php endif; ?>
can anybody help me with this ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信附件使用
post_parent
作为“链接”。所以你可以这样做:I believe attachments use
post_parent
as the "link". So you could do something like this: