未采用所有属性的库_短码函数的替换

时间:2011-05-18 作者:kaiser

我有以下自定义函数来替换gallery_shortcode 核心功能。它从一开始就起作用了。

The problem 我无法从其中的库快捷码中获取自定义键/值。我在核心函数中接收它们,但它们不会传递到我的自定义函数中。我真的无法调查到底出了什么问题,或者他们在哪里失踪了,因为我没有得到任何print_r 的输出$attr 在我的自定义函数中。有什么想法吗?

注意:我为从核心到自定义函数的所有更改添加了注释

function modified_post_gallery( $attr ) 
{
    // I can\'t get any output from the following
    echo \'<pre>\'; 
            print_r( $attr ); 
    echo \'</pre>\';
    // But: i get the expected output if i place the same line inside the core function: 
    /root/wp-include/media.php line 759 and get the actual output including all custom key/values.

    global $post, $wp_locale;

    static $instance = 0;
    $instance++;

    $output = \'\';

    // 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 result from user input & defaults
    extract( shortcode_atts( array(
         \'order\'        => \'ASC\'
        ,\'orderby\'      => \'menu_order ID\'
        ,\'id\'           => $post->ID
        ,\'itemtag\'      => \'span\' // CHANGED
        ,\'icontag\'      => \'span\' // CHANGED
        ,\'captiontag\'   => \'span\' // CHANGED
        ,\'columns\'      => 3
        ,\'size\'         => \'thumbnail\'
        ,\'include\'      => \'\'
        ,\'exclude\'      => \'\'
        ,\'class\'        => \'\' // ADDED
        ,\'test\'
    ), $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 
        ) );
    }

    // No attachments, so abort
    if ( empty( $attachments ) )
        return \'\';

    // RSS/Atom
    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;

    $gallery_div = \'\';

    $size_class = sanitize_html_class( $size );

    $gallery_div = \'<div id="\'.$selector.\'" class="gallery galleryid-\'.$id.\' gallery-columns-\'.$columns.\' gallery-size-\'.$size_class.\'">\';

    $i = 0;
    foreach ( $attachments as $id => $attachment ) 
    {
        $class = $columns > 0 && ++$i % $columns == 0 ? \' last\' : \'\'; // ADDED for the single item/image

        $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 \'.$class.\'">\'; // ADDED class
        $output .= \'<\'.$icontag.\' class="gallery-icon">\'.$link.\'</\'.$icontag.\'>\';
        if ( $captiontag && trim($attachment->post_excerpt) )
        {
            $output .= \'<\'.$captiontag.\' class="wp-caption-text gallery-caption">\';
            $output .= wptexturize( $attachment->post_excerpt );
            $output .= \'</\'.$captiontag.\'>\';
        }
        $output .= \'</\'.$itemtag.\'>\';
    }
    $output .= \'\'."\\n";

    return $output;
}
add_filter( \'post_gallery\', \'modified_post_gallery\', 10, 2 );

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

<?php
function modified_post_gallery( $blank = NULL, $attr ) 
{
    echo \'<pre>\'; 
            print_r( $attr ); 
    echo \'</pre>\';
}
add_filter( \'post_gallery\', \'modified_post_gallery\', 10, 2);
?>
在媒体中。php它显示如下过滤器:

// Allow plugins/themes to override the default gallery template.
$output = apply_filters(\'post_gallery\', \'\', $attr);
它传递的是两个变量而不是一个,而$attr位于第二个变量上。

结束

相关推荐

User-based media gallery

我正在建立一个同时使用WordPress和BuddyPress的网站,我希望建立一个自定义部分,用户可以上传作品,人们可以对其进行评论/评分。我浏览了各种不同的选项,但似乎使用自定义的帖子类型,然后允许成员提交内容是最好的选择。这是更好的方法吗?还是你认为帖子类型是最好的选择。要了解其所需的功能类型,请执行以下操作:http://dribbble.com/其想法是将图像作为类似域的URL。com/designs/my-awesome-work-2201/当查看成员档案时,您将能够查看他们的最新作品等,因此