我正在使用此代码在页面上有一个简单的图库:
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
现在的问题是最终用户必须先通过“媒体”页面上传图像,然后才能将此图像选为特色图像。
我知道这可以通过将特色图片的ID添加到短代码的排除列表中来解决,但是如何自动获取此ID?
答案 0 :(得分:15)
function exclude_thumbnail_from_gallery($null, $attr)
{
if (!$thumbnail_ID = get_post_thumbnail_id())
return $null; // no point carrying on if no thumbnail ID
// temporarily remove the filter, otherwise endless loop!
remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');
// pop in our excluded thumbnail
if (!isset($attr['exclude']) || empty($attr['exclude']))
$attr['exclude'] = array($thumbnail_ID);
elseif (is_array($attr['exclude']))
$attr['exclude'][] = $thumbnail_ID;
// now manually invoke the shortcode handler
$gallery = gallery_shortcode($attr);
// add the filter back
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
// return output to the calling instance of gallery_shortcode()
return $gallery;
}
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
答案 1 :(得分:3)
<?php $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
<?php echo do_shortcode('[gallery exclude='.$id.' link="file" itemtag="div" icontag="span" captiontag="p" size="thumbnail" columns="4" ]'); ?>
答案 2 :(得分:1)
怎么样?
echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]');