自定义WordPress库的html布局

时间:2021-03-10 作者:omid chahardoli

我想更改WordPress gallery的html布局,默认情况下,html的输出如下:

             <div id="gallery-1" class="gallery galleryid-184 gallery-columns-3 gallery-size-thumbnail">
               <dl class="gallery-item">
                 <dt class="gallery-icon landscape">
                          <a href="http://localhost:8888/mmysite/tesla-2/"><img src="http://localhost:8888/mmysite/wp-content/uploads/2021/03/tesla.png" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" width="150" height="110"></a>
                     </dt>
               </dl>
               <dl class="gallery-item">
                      <dt class="gallery-icon landscape">
                          <a href="http://localhost:8888/mmysite/3/"><img src="http://localhost:8888/mmysite/wp-content/uploads/2021/03/3.gif" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" width="150" height="58"></a>
                      </dt>
               </dl>
                    <br style="clear: both">
                </div>
我想改变一下:

             <div class="mihan-gallery">
                 <a class="mihangitem" href="http://localhost:8888/mmysite/wp-content/uploads/2021/03/tesla.png"><img src="http://localhost:8888/mmysite/wp-content/uploads/2021/03/tesla.png" alt="" title=""/></a>
                 <a class="mihangitem" href="http://localhost:8888/mmysite/wp-content/uploads/2021/03/3.gif"><img src="http://localhost:8888/mmysite/wp-content/uploads/2021/03/3.gif" alt="" title=""/></a>
                 <div class="clear"></div>
             </div>

1 个回复
最合适的回答,由SO网友:omid chahardoli 整理而成

我找到了一个解决方案:

// Customise the WordPress gallery html layout
add_filter(\'post_gallery\',\'customFormatGallery\',10,2);
function customFormatGallery($string,$attr){
    $output = "<div class=\\"mihan-gallery\\">";
    $posts = get_posts(array(\'include\' => $attr[\'ids\'],\'post_type\' => \'attachment\'));
    foreach($posts as $imagePost){
        $output .= "<a class=\\"mihangitem\\" href=\'".wp_get_attachment_image_src($imagePost->ID, \'mihan-large\')[0]."\'><img src=\'".wp_get_attachment_image_src($imagePost->ID, \'mihan-medium\')[0]."\'></a>";
    }
    $output .= "<div class=\\"clear\\"></div></div>";
    return $output;
}

相关推荐

Looking for Gallery Plugin

我正在寻找wordpress插件的小工作。我想有一个里面有图像的文件夹,我想把这个文件夹移到某个目录下,然后自动创建图库帖子。例如,我有一个名为“Winter 2018”的文件夹,里面有10张图片,移动该文件夹时,我想用这些图片中的特色图片和从文件夹“Winter 2018”中提取的标题自动创建新帖子是否有任何插件可以从目录文件夹创建自动帖子?