使用POST_QUALUARY将容器div添加到库

时间:2017-05-18 作者:fightstarr20

我正在尝试将容器div添加到WordPress库中,到目前为止,我有这个。。。。

function gallery_custom( $output ) {

    $return = \'<div class="mydiv">\';
    $return = $output;
    $return = \'</div>\';

    return $return;
}

add_filter( \'post_gallery\', \'gallery_custom\', 10, 3 );
这并没有返回任何东西,有什么想法我哪里出了问题吗?

2 个回复
SO网友:Dave Romsey

可以使用post_gallery 滤器下面是一个完全注释过的示例。

add_filter( \'post_gallery\', \'gallery_custom\', 10, 3 );
/**
 * Filters the default gallery shortcode output.
 *
 * If the filtered output isn\'t empty, it will be used instead of generating
 * the default gallery template.
 *
 * @see gallery_shortcode()
 *
 * @param string $output   The gallery output. Default empty.
 * @param array  $attr     Attributes of the gallery shortcode.
 * @param int    $instance Unique numeric ID of this gallery shortcode instance.
 */
function gallery_custom( $output, $attr, $instance ) {
    // Remove the filter to prevent infinite loop.
    remove_filter( \'post_gallery\', \'gallery_custom\', 10, 3 );

    // Add opening wrapper.
    $return = \'<div class="mydiv">\';

    // Generate the standard gallery output.
    $return .= gallery_shortcode( $attr );

    // Add closing wrapper.
    $return .= \'</div>\';

    // Add the filter for subsequent calls to gallery shortcode.
    add_filter( \'post_gallery\', \'gallery_custom\', 10, 3 );

    // Finally, return the output.
    return $return;
}

英寸gallery_custom(), 移除post_gallery 调用前筛选gallery_shortcode() 否则我们会陷入无限循环。

请注意,输出需要连接到$return. 在原始代码中,$return 持续被覆盖,因为= 使用而不是.= 字符串初始化后。

主输出是使用标准gallery输出功能生成的,gallery_shortcode( $attr ); 我们的筛选器将不会应用于此调用,因为此时已将其删除。

将库输出连接到$return, 我们添加结束HTML标记并添加回过滤器,以便在下次调用gallery shortcode函数时运行它。

  • 最后我们返回输出。

    备选解决方案:更换[gallery] 快捷码函数:这里有另一种解决问题的方法。这一次是默认的gallery输出功能,gallery_shortcode() 从中删除gallery 短代码字符串。然后是替换函数,wpse_custom_gallery_shortcode() 连接到原始gallery 短代码字符串。

    // Replace the default [gallery] shortcode function with a custom function.
    add_action( \'init\', \'wpse_replace_gallery_shortcode\' ); 
    function wpse_replace_gallery_shortcode() {
        remove_shortcode( \'gallery\', \'gallery_shortcode\' );
        add_shortcode( \'gallery\', \'wpse_custom_gallery_shortcode\' );
    }
    
    // Customized gallery shortcode function.
    // See gallery_shortcode() for documentation.
    function wpse_custom_gallery_shortcode( $attr ) {
        $gallery = gallery_shortcode( $attr );
    
        if ( $gallery ) {
            return \'<div class="mydiv">\' . $gallery . \'</div>\';
        }
    } 
    

  • SO网友:Vinod Dalvi

    要实现这一点,您必须开发更多的自定义代码,如下所示。

    function gallery_custom( $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 \'\';
    
        $output = \'<div class="mydiv">\';
    
        // Here\'s your actual output, you may customize it to your need
        $output .= "<div class=\'gallery galleryid-$columns gallery-columns-$columns gallery-size-$size\'>";
    
        // 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, $size );
    
            $output .= \'<figure class="gallery-item"><div class="gallery-icon landscape">\';
            $output .= \'<img src="\' . $img[0] . \'" width="\' . $img[1] . \'" height="\' . $img[2] . \'" alt="" />\';
            $output .= \'</div>\';
            if ( $captiontag && trim($attachment->post_excerpt) ) {
                $output .= "
                    <{$captiontag} class=\'gallery-caption\'>
                    " . wptexturize($attachment->post_excerpt) . "
                    </{$captiontag}>";
            }
            $output .= \'</figure>\';
        }
    
        $output .= \'</div></div>\';
    
        return $output;
    }
    
    add_filter( \'post_gallery\', \'gallery_custom\', 10, 2 );
    

    结束

    相关推荐

    Posts per page, reverse count

    假设我在我的博客上有10篇文章,每页最多7篇,这就是我得到的:第1页(主页):7篇文章第2页:3篇文章,但我需要这个:第1页(主页):3篇文章第2页:7篇文章我需要从最后一页开始发帖,你知道怎么做吗?