强制WordPress库使用媒体文件链接而不是附件链接

时间:2014-02-14 17:13:57

标签: php wordpress hook add-filter

是否有过滤器或挂钩强制WordPress图库使用媒体文件链接而不是附件链接。 我无法相信我的客户在创建图库时手动切换设置。 http://cl.ly/image/2g1L1G2c2222

提出另一个问题,即Media File不是默认类型。

2 个答案:

答案 0 :(得分:8)

您基本上可以使用默认的图库短代码。

remove_shortcode('gallery', 'gallery_shortcode'); // removes the original shortcode
    add_shortcode('gallery', 'my_awesome_gallery_shortcode'); // add your own shortcode

    function my_awesome_gallery_shortcode($attr) {
    $output = 'Codex is your friend' ;
    return $output;
}

在自定义功能中,您可以设置任何您想要的内容..查看original gallery shortcode

另一种方法是实际过滤属性(如果你将转到上面的链接,你会看到一个过滤器钩子)

add_shortcode( 'gallery', 'my_gallery_shortcode' );

function my_gallery_shortcode( $atts )
{
    $atts['link'] = 'file';
    return gallery_shortcode( $atts );
}

答案 1 :(得分:1)

Obmerk Kronen 的回答非常简洁,适用于大多数情况。但是,最近我在我客户的一个网站上遇到了一个问题,无法通过这种方式解决。我其实不知道,为什么。我想这是因为另一个图库插件篡改了相同的短代码。

所以,我找到了一个不同的解决方案,不是那么简洁,但有效。在这里,也许有人会觉得这很有帮助:

add_filter('the_content','galleries_attachments_to_media_links');
function galleries_attachments_to_media_links( $content ) {
/**
 * Overrides gallery shortcode settings and forces media file loading, if gallery set to attachment link
 */
if(strpos($content,'[gallery') !== FALSE) {
    $content = str_replace('[gallery','[gallery link="file"',$content);
    return $content;
}
else return $content;
}

看起来即使画廊已经设置为媒体文件,这也不是问题,WordPress 会正确执行短代码。