通过ACF将类添加到Wordpress库附件

时间:2018-02-16 07:33:09

标签: wordpress advanced-custom-fields

我想在Wordpress Gallery中为图像(附件)添加一个类(顺便说一句,奇怪的是,默认情况下这是不可能的),以便在Masonry Gallery中将它们置于全宽状态。我设置了一个带有复选框的ACF,可以勾选此选项,并生成字段" fullwidth" 我遇到的问题是我无法使用附件图像的wp过滤器将类注入适当的位置 - 生成的图库代码的级别。

应该是:

<div id="gallery-2" class="gallery galleryid-45 gallery-columns-2 gallery-size-large" >

  <dl class="gallery-item fullwidth">
    <dt class="gallery-icon">
      <img width="700" height="467" src="xy.jpg" class="attachment-large size-large">
</dt></dl>

  <dl class="gallery-item">
    <dt class="gallery-icon">
       <img width="700" height="525" src="xyz" class="attachment-large size-large" >
</dt></dl>

</div>

我在我的functions.php

中使用了这段代码
function addclass_gallery_img_atts( $atts, $attachment ) {

if( get_field('fullwidth') ) {
    $fullwidth = get_field( "fullwidth" );
    $atts['class'] = $fullwidth;
}    
    return $atts;
}
add_filter( 'wp_get_attachment_image_attributes', 'addclass_gallery_img_atts', 10, 2 );

0 个答案:

没有答案
相关问题