【发布时间】:2015-03-24 02:59:33
【问题描述】:
我试图制作一个模板来显示 wordpress 博客中所有使用过的图片,并链接到附有图片的帖子,例如画廊或投资组合页面,但我无法获得帖子的链接或标题页。 我已经做到了这一点:
<?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:
<?php foreach($attachments as $id => $attachment) :
$img_title = $attachment->post_title;
$img_caption = $attachment->post_excerpt;
$img_description = $attachment->post_content;
?>
所以我查阅了 codex 并找到了如何像这样获取 img url:
<?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 列表中附加的所有图像。
然后我想在循环中添加一个指向父 POST 的链接,在 foreach 中使用如下内容:
<p><a href="<?php echo get_post($id)->post_parent; ?>">Post link</a></p>
但它返回一个无链接或返回一个“0”,如果我“回显$id”,它返回附件ID而不是帖子ID(帖子ID是我想要的)。
请问如何获取父帖子 ID 或仅在上面的循环中获取帖子链接和标题?救命!
谢谢!
【问题讨论】:
-
仍然无法解决这个问题,谁能给我一个线索?