我有一个幻灯片,其中填充了自定义帖子类型的背景图像。这是我的代码:
<?php
// start the loop
foreach($slider_posts as $post) : setup_postdata($post);
// get image
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), \'home-slide\');
?>
<script type="text/javascript">
$(function(){
$.mbBgndGallery.buildGallery({
// containment:"#wrapper",
containment:"body",
timer:5000,
effTimer:5000,
controls:"#controls",
grayScale:false,
autoStart:true,
images:[
"<?php echo $thumbnail[0]; ?> "
],
});
});
</script>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php } ?>
此代码只显示一个幻灯片图像,而不是所有幻灯片的图像!
我怎样才能修复它?
SO网友:Shane
尝试移动您的foreach
进入脚本标记,如下所示:
在images
javascript的参数:
//[...]
images:[
<?php
foreach( $slider_posts as $post )
{
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), \'home-slide\');
echo \'"\' . $thumb[0] . \'",\'; //Assuming images are separated by a comma
}
?>
]
您可能需要稍微修改一下示例,使其正常工作。
Meta Example
foreach( $slider_posts as $post )
{
$meta = get_post_meta( $post->ID, \'the-meta-field-name\', true ); //replace true with false if you want an array of meta field results
}