从帖子显示wordpress图像

时间:2013-06-23 17:36:46

标签: wordpress

我要做的是为我的客户创建一个投资组合。所以我有一个自定义的帖子,她可以添加一个艺术品和图片。

但是,我希望它以幻灯片形式显示,我可以在幻灯片中显示特色图像,但我要在幻灯片中显示该特定艺术品的所有图像,因此有人可以翻阅它们。

这是我目前的代码:

<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item"><?php the_post_thumbnail( 'wpbs-featured' ); ?></div>
<div class="item"><?php echo wp_get_attachment_image( 1 ); ?></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
</div>

所以我抓住特色图片,但需要其他图片显示在特定帖子的幻灯片中。

我尝试过使用wp_get_attachment_image但是我怎么知道ID是什么?e并且ID不需要是动态的,因为每个新的艺术作品都是不同ID的不同帖子吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

当你在循环中时,你可以使用

$images = get_children( array (
    'post_parent' => $post->ID,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

if ( count($images) ) {
    $size = 'thumbnail';  // medium, large
    $output = '';
    foreach ( $images as $id => $attachment ) {
        $title = esc_html( $attachment->post_title, 1 );
        $img = wp_get_attachment_image_src( $id, $size );
        $output .= '<a class="selector $size" href="' . esc_url( wp_get_attachment_url( $id ) ) . '" title="' . esc_attr( $title ) . '">';
        $output .= '<img class="aligncenter" src="' . esc_url( $img[0] ) . '" alt="' . esc_attr( $title ) . '" title="' . esc_attr( $title ) . '" />';
        $output .= '</a>';
    }
   // Now do something with $output
   // which is collection of images wrapped with <a> from one post;
   // something like
   // <a href="..." title="..."><img src="..." alt="..." title="..." /></a>
   // <a href="..." title="..."><img src="..." alt="..." title="..." /></a>
}

在循环中,$post->ID会为您提供帖子ID,请注意'post_parent' => $post->ID,函数中的get_children

Check on Codex