Wordpress 从附件中获取帖子信息 [英] Wordpress get Post infomation from attachment

查看:25
本文介绍了Wordpress 从附件中获取帖子信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个模板来显示 wordpress 博客中所有使用过的图片,并链接到附有图片的帖子,如画廊或投资组合页面,但我无法获得帖子的链接或标题页.我已经做到了这一点:

I was trying to make a template to show all used image in wordpress blog, and link to the post which has the image attached, like a gallery or portfolio page, but I can not get the link nor title to the post single page. I've made this far:

 <?php
$post_parent = get_post($post->ID, ARRAY_A);
$parent = $post_parent['post_parent'];
$paged = ( get_query_var( 'paged' ) ) ? get_query_var('paged') : 1;
$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC', 'paged' => $paged, 'posts_per_page' =>9,) ); 
?>

还有 foreach:

And the foreach:

<?php foreach($attachments as $id => $attachment) :
$img_title = $attachment->post_title;
$img_caption = $attachment->post_excerpt;
$img_description = $attachment->post_content;
?>

于是我查了codex,找到了这样获取img url的方法:

So I looked up the codex and found how to get the img url like this:

<?php if ( have_posts() ) : ?>
<li>
<?php $image_attributes = wp_get_attachment_image_src( $id, full ); // returns an array
if( $image_attributes ) {?> 
<a  href="<?php echo $image_attributes[0]; ?>"><img src="<?php echo $image_attributes[0]; ?>" alt="<?php echo $img_alt; ?>" title="<?php echo $img_title; ?>" /></a>
<?php
echo $post->post_parent;
?>
<p><a href="<?php
echo get_post($id)->post_parent; ?>">link</a></p>
<?php } ?>
</li>
<?php endif; ?>

<?php endforeach; ?>

上面的这些代码效果很好,它显示了附加在 ul li 列表中的所有图像.

These codes above works great, it shows all images attached in an ul li list.

然后我想在循环中添加一个指向父 POST 的链接,在 foreach 中使用类似的东西:

Then I want to add a link to the parent POST in the loop, using something in the foreach like:

<p><a href="<?php echo get_post($id)->post_parent; ?>">Post link</a></p>

但它返回一个没有链接的链接或返回一个0",如果我echo $id",它返回附件 ID 而不是帖子 ID(帖子 ID 是我想要的).

but it returns a link to nothing or returns a "0", if I "echo $id", it return the attachment id but not the Post ID (Post id is what I want).

请问如何获取父帖子 ID 或只是获取上面循环中的帖子链接和标题?帮助!

Please how to get the parent post ID or simply get the post link and title in the loop above? Help!

谢谢!

推荐答案

立即找到答案.我使用普通查询来获取所有帖子,然后该查询在内容(或其特色图片)中搜索 img 标签,然后循环显示该图片,可选择在该图片下方显示摘录,然后此模板就完成了.

Find the answer now. I use the normal query to get all posts, and that query search for the img tag in content( or its featured image) then show that image in loop, optionally to show excerpt below that image, then this template is done.

我想在媒体库中获取附件,但这不是正确的方法,有些媒体甚至没有附加到任何帖子中,所以这是一个死胡同.

I was thinking about getting attachments in media library but it is not the right way, some media are not even attached to any posts so it’s a dead end.

但是查询帖子并使该查询选择带有<img"的帖子比查询附件要好得多,而且也易于理解.

But to query post and make that query select posts with "<img" is somewhat way more better than query attachments, and easy to understand too.

这篇关于Wordpress 从附件中获取帖子信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆