我会重写一个简单的短代码,只检索帖子图像:
add_shortcode( "postImages", "stack_309224_attachhment" );
function stack_309224_attachhment($atts){
extract(shortcode_atts( array(
\'id\' => 1, // default id of the post
"max" =>4 ,// default max number of images to display
"featured"=>"false" // if to retrieve the post featured image or not, default false
), $atts ));
ob_start();
if($atts[\'featured\']=="true"){ // display only the featured image
?>
<figure><img src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id( (int)$atts[\'id\']) )[0]?>" /></figure>
<?php
}
else{ // display a gallery of attached images within "max" attr.
$images=get_attached_media( \'image\', (int)$atts[\'id\'] );
if(count($images) > 0 ){
?>
<div class="gallery">
<?php
$index=0;
foreach($images as $image){
if($index < (int)$atts[\'max\']){ //display image only if within max
?>
<figure><img src="<?php echo wp_get_attachment_url($image->ID)?>" /></figure>
<?php
$index ++;
}
}
?></div><?php
}
}
return ob_get_clean();
}
然后在wp编辑器中,您可以使用:
[postImages id=2387 featured=“true”]->仅显示post的特征图像#2387
[postmages id=2387 max=4]>显示附加到post#2387的4幅图像的库