从图库简码中排除 the_post_thumbnail [英] Exclude the_post_thumbnail from gallery shortcode

查看:12
本文介绍了从图库简码中排除 the_post_thumbnail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码在页面上创建一个简单的图库:

I am using this code to have a simple gallery on the page:

<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>

现在的问题是,最终用户必须先通过媒体页面上传图片,然后才能选择该图片作为特色图片.

The problem now is that the end-user has to upload an image via the Media page before selecting this image as featured image.

我知道这可以通过将特色图片的 ID 添加到短代码的排除列表中来解决,但如何自动获取此 ID?

I know this could be solved by adding the featured image's ID to the shortcode's exclude list, but how to get this ID automatically?

推荐答案

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);

这篇关于从图库简码中排除 the_post_thumbnail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆