WordPress图库‘POST_QUALUARY’筛选器不能处理提要?

时间:2012-09-03 作者:its_me

我们可以use the post_gallery filter 修改/替换默认WordPress库模板(又名输出代码),以满足我们的需要。但这似乎不会影响RSS提要中的标记。

无论我做什么,RSS提要中画廊图像的标记都是这样的:

<a href="#1">
   <img src="#2" />
</a>

<a href="#3">
   <img src="#4" />
</a>
那么,有没有办法(比如过滤器、函数等)修改RSS提要中的默认WordPress库标记?

我一开始就试过(只是为了检查一下),我所要做的就是在wp-includes/media.php 核心文件:

if ( is_feed() ) {
    $output = "\\n";
    foreach ( $attachments as $att_id => $attachment )
        $output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
    return $output;
}
而且还挺管用的。现在,我的RSS提要中的库标记与您在我的网站前端(即在帖子中)上看到的相同。

但当我做同样的事using the post_gallery filter, 它不起作用RSS提要中的标记不受影响。我不知道为什么!

总的来说this is the code (与post_gallery) 在我的功能中。php。我做错什么了吗?

由于经常有人问我在这种情况下要做什么,我想修改RSS提要中WordPress库的标记,如下所示:

<section class="fl-slideshow">
  <figure>
    <img src="#2" width="1200" height="900">
    <figcaption>Puppies are cute</figcaption>
  </figure>
  <figure>
    <img src="#4" width="900" height="1200">
    <figcaption>Kittens are too</figcaption>
  </figure>
</section>

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

如果不知道post\\u gallery filter函数的作用,就无法给出正确的答案。

但是,如果您正在使用post\\u gallery过滤器并返回新标记,那么是的,它也将在提要中使用。您提到的注释掉的代码发生在post\\u gallery过滤器之后,如果您从该过滤器返回不同的输出,则根本不会执行,如下代码所示:

// Allow plugins/themes to override the default gallery template.
$output = apply_filters(\'post_gallery\', \'\', $attr);
if ( $output != \'\' )
    return $output;
如果post\\u gallery过滤器返回任何内容,那么这就是输出,period。您提到的if is\\u feed代码将不适用,因为它从未达到如此程度。

检查post\\u gallery挂钩函数。问题可能就在那里的某个地方。

Edit: 在帖子底部找到了代码的链接。这有点隐蔽。

你有两个主要问题。首先,您在自己的函数中有if is\\u提要代码,注释它确实是正确的做法。函数应该生成输出。如果您正在生成不同的输出,那么您显然需要改变这一点。

其次,这是不正确的:

add_shortcode(\'post_gallery\', \'flipboard_gallery_shortcode\', 10, 2);
post\\u gallery挂钩是过滤器挂钩,而不是短代码。将该行更改为:

add_filter(\'post_gallery\', \'flipboard_gallery_shortcode\', 10, 2);

SO网友:its_me

根据奥托的建议,我修改了我的函数(见下文),它成功了!

// Custom Gallery Code For Flipboard/Pulse/Google Currents Feeds
add_filter(\'post_gallery\', \'flipboard_gallery_shortcode\', 10, 2);
function flipboard_gallery_shortcode($output, $attr) {
    global $post;

    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\'       => \'full\',
        \'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 \'\';

    // Modifying for a different gallery output ONLY in my custom feed
    if ( is_feed( \'flipboard_feed\' ) ) {
        $output = \'<section class="fl-slideshow">\';
        foreach ( $attachments as $att_id => $attachment )
            $output .= \'<figure>\' . wp_get_attachment_image($att_id, \'full\') . \'<figcaption>\' . wptexturize($attachment->post_excerpt) . \'</figcaption></figure>\';
        $output .= \'</section>\';
        return $output;
    }

    // Let the original function return the default output for other feeds and posts
    return \'\';
}
EDIT: 为了稍微提高速度,可以将上面的代码重写为(感谢Otto!):

// Custom Gallery Code For Flipboard/Pulse/Google Currents Feeds
add_filter(\'post_gallery\', \'flipboard_gallery_shortcode\', 10, 2);
function flipboard_gallery_shortcode($output, $attr) {

    // Modifying for a different gallery output ONLY in my custom feed
    if ( is_feed( \'flipboard_feed\' ) ) {
        global $post;

        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\'       => \'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];
            }
        } 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 \'\';

            // Essentially these are only changes I\'ve made
            $output = \'<section class="fl-slideshow">\';
            foreach ( $attachments as $att_id => $attachment )
                $output .= \'<figure>\' . wp_get_attachment_image($att_id, \'full\') . \'<figcaption>\' . wptexturize($attachment->post_excerpt) . \'</figcaption></figure>\';
                $output .= \'</section>\';
            return $output;
    }

    // Let the original function return the default output for other feeds and posts
    return \'\';

}
PS: 任何使用Jetpack Carousel的人都不必担心。上面的函数只修改自定义提要中的输出,因此就我所见,它不会影响任何前端插件

SO网友:kaiser

应该是这么简单。。。

function wpse63980_gallery_shortcode( $html, $attr )
{
    // We don\'t want to intercept non feed galleries
    if ( ! is_feed() )
        return \'\';

    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 ) );

    // get_posts(), get_attachments(), etc. here

    return $html;
}
add_filter( \'post_gallery\', \'wpse63980_gallery_shortcode\', 99999, 2 );
。。。当第二个参数不是\'\'. 如您所见,我为过滤器添加了一个非常高的优先级,以防其他插件出错。

结束

相关推荐

no content after shortcode

我希望你们能帮我解决我遇到的这个小错误。我正在使用一个快捷码在条目中显示音频播放器。这是我在函数中输入的代码。php:function html5_audio($atts, $content = null) { extract(shortcode_atts(array( \"src\" => \'\', \"preload\"=> \'none\', \"loop\" => \'\' ), $atts)); retu