在3.5中自定义图库快捷代码

时间:2012-12-16 作者:gzhmr

我正在尝试使用3.5中新的gallery系统自定义gallery快捷码,但我遇到了一些问题,无法找出哪里出了问题。这就是我现在所拥有的(in functions.php):

include( \'lib/theme-setup.php\' );
include( \'lib/load-scripts.php\' );

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

    // 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\'] );
    endif;

    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'columns\'    => 0,
        \'size\'       => \'medium\',
        \'include\'    => \'\'
    ), $attr));

    $id = intval($id);

    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];
        }
    } 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;
    }

    $output .= \'<div id="gallery-scroller">\';
    $i = 1;

    foreach ( $attachments as $id => $attachment ) :

        $attimg = wp_get_attachment_image( $id, $size );
        $atturl = wp_get_attachment_image_src( $id, $size );
        $output .= \'<img src="\'.$atturl[0].\'" alt="Demo image" id="\'.$i.\'" />\';
        $i++;

    endforeach;

    $output .= \'</div>\';

    return $output;
}
然后,我只有一个简单的自定义页面模板,该模板显示循环中引用“the\\u content()”的库,以输出库短代码:

if (have_posts()) : while (have_posts()) : the_post(); 
    the_content(); 
endwhile; endif;
图像会显示出来,但由于某种原因,库的javascript功能无法以这种方式工作。问题必须出在短代码上,因为奇怪的是,如果我在自定义页面模板中去掉循环,只添加一个函数来检索页面的附件,而不是使用短代码,javascript工作和加载都很好。但问题是,它会拉入附加到该页面的所有图像,而我只想要库中的图像,用于库短代码完成的特定页面。我真的很喜欢3.5版的新gallery界面,并希望能够为我的gallery实现此功能。

关于这可能是什么原因有什么建议吗?

1 个回复
SO网友:Cosmin Pascu

确保加载脚本加载的javascript。php正在等待文档完成加载,然后再附加所有侦听器。

当您创建一个单独的函数时,代码运行的时间只提前了一点点,但对于元素完成加载和监听器正确连接来说,可能已经足够早了。

结束