1

我创建了自定义幻灯片帖子类型。图像设置为每个新幻灯片帖子的特色图像。我想在幻灯片模板中检索这些图像。

此幻灯片模板的 HTML 标记为:

<div class="wrapper">
<ul id="my-slider" class="my-slider">

<li>
<a href="http://www.flickr.com/photos/photo/123456" target="_blank"><img src="images/1.jpg" alt="image1"/></a>
<div class="my-description">
<h3>Image one</h3>
</div>
</li>

<li>
<a href="http://www.flickr.com/photos/photo/1234565" target="_blank"><img src="images/2.jpg" alt="image2"/></a>
<div class="my-description">
<h3>Image two</h3>
</div>
</li>


<li>
<a href="http://www.flickr.com/photos/photo/12345655" target="_blank"><img src="images/3.jpg" alt="image3"/></a>
<div class="my-description">
<h3>Image three</h3>
</div>
</li>

<li>
<a href="http://www.flickr.com/photos/photo/12345666" target="_blank"><img src="images/4.jpg" alt="image4"/></a>
<div class="my-description">
<h3>Image four oner</h3>
</div>
</li>
</ul>
</div>

当我对其进行硬编码时,此 HTML 提供了四个幻灯片图像。如何从 wordpress 自定义帖子类型中动态检索附加图像以获得相同的结果?

4

3 回答 3

3

为什么不简单地查询您的自定义帖子类型?一个例子:

<?php
$args = array('post_type' => 'your_custom_post_type');
query_posts( $args );

// the Loop
while (have_posts()) : the_post();
  //Do your stuff  

  //You can access your feature image like this:
  the_post_thumbnail();
endwhile;
于 2012-12-26T12:24:09.877 回答
2
Below is the example to retrive feature image. 

$args = array('post_type' => 'your_custom_post_type');
query_posts( $args );

// the Loop
while (have_posts()) : the_post();

if ( has_post_thumbnail() ) {

//Image size small,large or medium
  the_post_thumbnail('thumbnail',imagesize);?>


}
?>
endwhile;
于 2012-12-26T13:47:39.070 回答
1

我通过从自定义帖子类型中获取所有图像的数组并将其存储在变量 $myimage 中来实现这一点。$myimage[0] 是 img 标签的 src,需要在循环中捕获以获取所有图像

global $post;
$args = array(
'post_type' =>'slideshow',
'numberposts' => -1,
'orderby' => 'menu_order' );

$slider_posts = get_posts($args); ?>
<?php if($slider_posts) { ?>
<?php // start the loop
foreach($slider_posts as $post) : setup_postdata($post);
$myimage = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
于 2012-12-27T12:50:15.057 回答