更改Wordpress默认图库输出

时间:2013-11-06 01:04:05

标签: wordpress zurb-foundation

我希望使用Wordpress gallery shortcut,但我希望将输出绑定到Foundation Orbit plugin(制作滑块)。这是我想要输出的HTML:

<div class="slideshow-wrapper">
    <div class="preloader"></div>
    <ul data-orbit>
        <li>
            <img src="img1.png" alt="bla bla bla" />
        </li>
        <li>
            <img src="img2.png" alt="bla bla bla" />
        </li>
        <li>
            <img src="img3.png" alt="bla bla bla" />
        </li>
        <li>
            <img src="img4.png" alt="bla bla bla" />
        </li>
    </ul>
</div>

是否可以在functions.php(或类似)中放置一些内容来实现此目的?

5 个答案:

答案 0 :(得分:75)

是的,的确如此。很久以前我发现了这段代码并且从那时起就一直在使用它。将WP的默认图库自定义为您想要的任何内容都很棒。

post_gallery的过滤器,您可以使用它来自定义所有默认WP画廊。这是我使用的代码示例,适用于您提供的模板。我尽可能地清理了它。

该功能的第一部分几乎是图库附件处理,因此您可能只想更改后半部分,即确定图库模板输出的部分(按照评论):

add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) {
    global $post;

    if (isset($attr['orderby'])) {
        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
        if (!$attr['orderby'])
            unset($attr['orderby']);
    }

    extract(shortcode_atts(array(
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'id' => $post->ID,
        'itemtag' => 'dl',
        'icontag' => 'dt',
        'captiontag' => 'dd',
        'columns' => 3,
        'size' => 'thumbnail',
        'include' => '',
        'exclude' => ''
    ), $attr));

    $id = intval($id);
    if ('RAND' == $order) $orderby = 'none';

    if (!empty($include)) {
        $include = preg_replace('/[^0-9,]+/', '', $include);
        $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

        $attachments = array();
        foreach ($_attachments as $key => $val) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    }

    if (empty($attachments)) return '';

    // Here's your actual output, you may customize it to your need
    $output = "<div class=\"slideshow-wrapper\">\n";
    $output .= "<div class=\"preloader\"></div>\n";
    $output .= "<ul data-orbit>\n";

    // Now you loop through each attachment
    foreach ($attachments as $id => $attachment) {
        // Fetch the thumbnail (or full image, it's up to you)
//      $img = wp_get_attachment_image_src($id, 'medium');
//      $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
        $img = wp_get_attachment_image_src($id, 'full');

        $output .= "<li>\n";
        $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
        $output .= "</li>\n";
    }

    $output .= "</ul>\n";
    $output .= "</div>\n";

    return $output;
}

只需将其粘贴到您的functions.php文件中并进行修改即可根据您的需要进行调整。我很确定它会为你工作,因为它对我有用:)

答案 1 :(得分:20)

超级回答Mathielo。

但是,我需要选择包含标题,因此我修改了您的代码以使用wp_prepare_attachment_for_js()函数,因为它似乎提供了必要的数据。

add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) {
global $post;

if (isset($attr['orderby'])) {
    $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
    if (!$attr['orderby'])
        unset($attr['orderby']);
}

extract(shortcode_atts(array(
    'order' => 'ASC',
    'orderby' => 'menu_order ID',
    'id' => $post->ID,
    'itemtag' => 'dl',
    'icontag' => 'dt',
    'captiontag' => 'dd',
    'columns' => 3,
    'size' => 'thumbnail',
    'include' => '',
    'exclude' => ''
), $attr));

$id = intval($id);
if ('RAND' == $order) $orderby = 'none';

if (!empty($include)) {
    $include = preg_replace('/[^0-9,]+/', '', $include);
    $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

    $attachments = array();
    foreach ($_attachments as $key => $val) {
        $attachments[$val->ID] = $_attachments[$key];
    }
}

if (empty($attachments)) return '';

// Here's your actual output, you may customize it to your need
$output = "<div class=\"slideshow-wrapper\">\n";
$output .= "<div class=\"preloader\"></div>\n";
$output .= "<ul data-orbit>\n";

// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
    // Fetch all data related to attachment 
    $img = wp_prepare_attachment_for_js($id);

    // If you want a different size change 'large' to eg. 'medium'
    $url = $img['sizes']['large']['url'];
    $height = $img['sizes']['large']['height'];
    $width = $img['sizes']['large']['width'];
    $alt = $img['alt'];

    // Store the caption
    $caption = $img['caption'];

    $output .= "<li>\n";
    $output .= "<img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" />\n";

    // Output the caption if it exists
    if ($caption) { 
        $output .= "<div class=\"orbit-caption\">{$caption}</div>\n";
    }
    $output .= "</li>\n";
}

$output .= "</ul>\n";
$output .= "</div>\n";

return $output;
}

答案 2 :(得分:3)

我知道原来的问题已经得到解答,但我只是想分享我对过滤器代码段所做的事情,以防它帮助其他任何人。我已经启用了Miro Mannino的“Justified Gallery&#39; jquery插件http://miromannino.com/projects/justified-gallery/与Wordpress 3.9中的Wordpress图库一起使用...这里的代码包含我为使其工作所做的更改...(img size light thumb是我的自定义缩略图以保留图像尺寸,但保持页面加载时间。)

// Custom Gallery

add_filter( 'post_gallery', 'my_post_gallery', 10, 2 );
function my_post_gallery( $output, $attr) {
global $post, $wp_locale;

static $instance = 0;
$instance++;

// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
    $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
    if ( !$attr['orderby'] )
        unset( $attr['orderby'] );
}

extract(shortcode_atts(array(
    'order'      => 'ASC',
    'orderby'    => 'menu_order ID',
    'id'         => $post->ID,
    'itemtag'    => 'dl',
    'icontag'    => 'dt',
    'captiontag' => 'dd',
    'columns'    => 3,
    'size'       => 'light-thumb',
    'include'    => '',
    'exclude'    => ''
), $attr));

$id = intval($id);
if ( 'RAND' == $order )
    $orderby = 'none';

if ( !empty($include) ) {
    $include = preg_replace( '/[^0-9,]+/', '', $include );
    $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

    $attachments = array();
    foreach ( $_attachments as $key => $val ) {
        $attachments[$val->ID] = $_attachments[$key];
    }
} elseif ( !empty($exclude) ) {
    $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
    $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
    $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}

if ( empty($attachments) )
    return '';

if ( is_feed() ) {
    $output = "\n";
    foreach ( $attachments as $att_id => $attachment )
        $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
    return $output;
}

$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';

$selector = "gallery-{$instance}";

$output = apply_filters('gallery_style', "
    <style type='text/css'>
        #{$selector} {
            margin: auto;
        }
        #{$selector} .gallery-item {
            float: {$float};
            margin-top: 0px;
            text-align: center;
            width: {$itemwidth}%;           }
        #{$selector} img {
            border: 0;
        }
        #{$selector} .gallery-caption {
            margin-left: 0;
        }
    </style>
    <!-- see gallery_shortcode() in wp-includes/media.php -->


    <div id='$selector' class='gallery galleryid-{$id}'>");
    $output = "<div id=\"mygallery\">\n";




$i = 0;
foreach ( $attachments as $id => $attachment ) {
    $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);


    $output .= "<{$itemtag} class='gallery-item'>";
    $output .= "
        <{$icontag} class='gallery-icon'>
            $link
        </{$icontag}>";
    if ( $captiontag && trim($attachment->post_excerpt) ) {
        $output .= "
            <{$captiontag} class='gallery-caption'>
            " . wptexturize($attachment->post_excerpt) . "
            </{$captiontag}>";
    }
    $output .= "</{$itemtag}>";
    if ( $columns > 0 && ++$i % $columns == 0 )
        $output .= '<br style="clear: both" />';
}

$output .= "
    <br style='clear: both;' />
    </div>\n";


return $output;

}

它是一种享受。感谢您分享过滤器 - 这正是我所寻找的。

答案 3 :(得分:1)

因此,如果您想要输出另一个字符串,如img title或img desc,请使用此构造

$ title = $ img ['title'];

这是评论超级回答Mathielo(第二个答案),这个通用解决方案,不仅仅是zubr基础

答案 4 :(得分:0)

小笔记!如果未在管理区域中禁用,则此过滤器将导致图库在管理区域中不可见。为了避免这种情况,我们可以在if语句中运行过滤器主要部分

add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr)
{
    // Disable function in admina area. 
    if (is_admin()) {
        return;
    } else {
      // put main code in here
    }
}