wordpress相册gallery的几种不同调用方式
技术文章 2024年1月6日
常规方式调用
if ( $gallery = get_post_gallery( get_the_ID() ) ) :
echo $gallery;
endif;
调用循环中的所有图片并可以自字义样式
if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
foreach ( $gallery['src'] AS $src ) {
echo '<img src="' . $src . '" class="my-custom-class" alt="Gallery image" />';
}
endif;
调用相册的ID
if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
$image_ids = explode( ',', $gallery['ids'] );
print_r( $image_ids );
endif;
带描述的相册调用
if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
foreach ( $gallery['src'] as $key => $src ) {
echo '<img src="' . $src . '" alt="' . $gallery['caption'][ $key ] . '" />';
}
endif;
显示相册图片总数
if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
$total_images = count( $gallery['src'] );
echo 'Total Images: ' . $total_images;
endif;