我使用這個代碼在頁面上簡單的畫廊:排除從畫廊簡碼the_post_thumbnail
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
現在的問題是,最終用戶能夠通過媒體頁面選擇此之前上傳圖片圖像作爲特色圖像。
我知道這可以通過將特色圖片的ID添加到短代碼的排除列表來解決,但是如何自動獲取此ID?
我使用這個代碼在頁面上簡單的畫廊:排除從畫廊簡碼the_post_thumbnail
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
現在的問題是,最終用戶能夠通過媒體頁面選擇此之前上傳圖片圖像作爲特色圖像。
我知道這可以通過將特色圖片的ID添加到短代碼的排除列表來解決,但是如何自動獲取此ID?
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);
<?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" ]'); ?>
怎麼樣?
echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id($post->ID) . '"]');
起初我以爲它像一個魅力工作,但我發現它排除了我所有的圖像,並返回一個值'數組'。 – Ewald 2010-12-04 12:26:30
我的不好 - 完全誤讀了`gallery_shortcode()`中的過濾器 - 檢查修改後的答案! – TheDeadMedic 2010-12-04 18:29:19