【问题标题】:Output gallery shortcode as bootstrap columns in Wordpress输出库简码作为 Wordpress 中的引导列
【发布时间】:2016-03-03 12:53:17
【问题描述】:

如何更改画廊简码的输出?在帖子中我们得到:

[gallery ids="134,127,109"]

我需要类似的东西:

<div class="row gallery">
    <div class="col-md-3">
        <a href="photo.jpg" data-gallery="gallery">
            <img src="photo.jpg" class="img-responsive" />
        </a>
    </div>
    . . . 
</div>

这必须在 the_content() 中起作用,因此必须替换图库过滤器功能。找到了一些关于它的主题,但都太旧了,所以功能发生了变化。如果该函数将尊重在画廊中选择的列数并将其更改为引导类,那将是很好的。 有人可以帮我吗?

【问题讨论】:

  • 那么你有什么尝试?哪些功能发生了变化?我给你一个提示:使用post_gallery filter
  • 我曾尝试使用 remove_shortcode / add_shortcode 来使用我自己的函数,但旧示例告诉要通过特定行从原始 media.php 复制某些内容,但在我的文件中我认为它不正确。找到函数 get_post_gallery 但我认为这没有返回列数

标签: php wordpress twitter-bootstrap gallery


【解决方案1】:

这应该对你有帮助:

add_filter( 'post_gallery', 'bootstrap_gallery', 10, 3 );

function bootstrap_gallery( $output = '', $atts, $instance )
{
    $atts = array_merge(array('columns' => 3), $atts);

    $columns = $atts['columns'];
    $images = explode(',', $atts['ids']);

    $col_class = 'col-md-4'; // default 3 columns
    if ($columns == 1) { $col_class = 'col-md-12';}
    else if ($columns == 2) { $col_class = 'col-md-6'; }
    // other column counts

    $return = '<div class="row gallery">';

    $i = 0;

    foreach ($images as $key => $value) {

        if ($i%$columns == 0 && $i > 0) {
            $return .= '</div><div class="row gallery">';
        }

        $image_attributes = wp_get_attachment_image_src($value, 'full');

        $return .= '
            <div class="'.$col_class.'">
                <a data-gallery="gallery" href="'.$image_attributes[0].'">
                    <img src="'.$image_attributes[0].'" alt="" class="img-responsive">
                </a>
            </div>';

        $i++;
    }

    $return .= '</div>';

    return $return;
}

【讨论】:

  • 它会覆盖默认的 worpress 过滤器吗?
  • 它将覆盖默认的 WordPress 画廊..只需测试它。
  • @bitWorking 我得到通知:未定义变量:col_class
  • @soniamaklouf 我编辑了这篇文章。现在有一个默认的 3 列定义。
【解决方案2】:

@bitWorking 的解决方案非常好 - 我知道这只是一个快速提示和启动代码,所以这不是抱怨缺少的东西。我只是对其进行了一些修改以修复/添加困扰我的东西-也许它对某人有用:modified code on GitHub

修复/添加“缺失”功能:

  1. 生成用户通过 WordPress 画廊面板设置的缩略图大小。
    • 使用全尺寸图片会浪费带宽。
    • 如果原始图像大小不同,库可能看起来不太好。
    • 如果 CMS 用户设置了特定的缩略图大小,并且画廊会显示不同的大小/比例,他/她可能会感到困惑。
  2. 生成缩略图alt 用户通过 WordPress 画廊面板设置的属性。
  3. 使缩略图链接(通过 WordPress 画廊面板设置)正常工作(无、附件页面、文件)以保持面板的功能并且不会混淆 CMS 用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多