如何对WordPress[图库]快捷代码进行分页?

时间:2013-03-05 作者:Ruriko

我正在使用wordpress gallery快捷码[图库],我想知道是否有任何方法可以不使用插件对其分页。

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

我已经写信答复了How to paginate attachments in a secondary query as gallery?, 可以用来解决这个问题。就像@wpmudev ari说的那样,[画廊]的短代码可以重写,或者可以创建一个新的短代码。我在下面解释-B-如何做到这一点。此外,还有一个非常简单的解决方案-a-用于短代码的分页。


A、 分页[图库]快捷码的简单解决方案

您可以通过以下方式实现分页:

numberpostsoffset 短代码的参数<!--nextpage--> 标签

一零

[gallery numberposts="9"]
<!--nextpage-->
[gallery offset="9" numberposts="9"]
 这实际上并不分页库,而是分页包含库的帖子,或者确切地说是拆分的库。但在某些用例中,这可能就足够了,请参阅源代码以了解更多详细信息。

来源:Quick Tip: Paginate Your WordPress Gallery


B、 通过重写[图库]或创建新的短代码来分页,假设您需要这样的内容:

|---------------------|   
|       content       |  
|       (static)      |  
|---------------------|  
|       gallery       |  
|       (paged)       |  
|---------------------|  
|   << pagelinks >>   |  
|---------------------|  

1。重写[库]短代码时,请使用a)或b),而不是同时使用两者

a)

Code:

remove_shortcode(\'gallery\', \'gallery_shortcode\');
add_shortcode(\'gallery\', \'wpse89462_get_attachment_gallery_shortcode\');

b)

Code:

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

2。创建新的短代码如果要保留原始短代码,请创建新的短代码

Code:

add_shortcode(\'wpse89462_paginated_attachment_gallery\', \'wpse89462_get_attachment_gallery_shortcode\');

3。短码函数如果找到短码,此函数将运行

Code:

function wpse89462_get_attachment_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\'] );
    }

    $html5 = current_theme_supports( \'html5\', \'gallery\' );
    extract(shortcode_atts(array(
        \'posts_per_page\' => 1, 
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post ? $post->ID : 0,
        \'itemtag\'    => $html5 ? \'figure\'     : \'dl\',
        \'icontag\'    => $html5 ? \'div\'        : \'dt\',
        \'captiontag\' => $html5 ? \'figcaption\' : \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'post__in\'    => \'\',
        \'post__not_in\'    => \'\'
    ), $attr));

    if ( ! empty( $post__in ) )
        $post__in = explode( \',\', $post__in );

    if ( ! empty( $post__not_in ) )
        $post__not_in = explode( \',\', $post__not_in );

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

    $output = \'\';

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

    $gallery_style = $gallery_div = \'\';
    if ( apply_filters( \'use_default_gallery_style\', ! $html5 ) ) {
        $gallery_style = "
         <style type=\'text/css\'>
                #{$selector} {
                       margin: auto;
                }
                #{$selector} .gallery-item {
                        float: {$float};
                        margin-top: 10px;
                        text-align: center;
                        width: {$itemwidth}%;
                 }
                 #{$selector} img {
                        border: 2px solid #cfcfcf;
                 }
                 #{$selector} .gallery-caption {
                        margin-left: 0;
                 }
                 #{$selector}-pag-nav {
                 }
        </style>";
    }
    $size_class = sanitize_html_class( $size );
    $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
    $gallery_nav_div = "<div id=\'{$selector}-pag-nav\' class=\'gallery galleryid-{$id} gallery-size-{$size_class}\'>";

    $gallery_page = (get_query_var(\'gallery_page\')) ? get_query_var(\'gallery_page\') : 1;

    $args = array(
        \'posts_per_page\' => $posts_per_page, 
        \'order\' => $order, 
        \'orderby\' => $orderby, 
        \'post_type\' => \'attachment\',
        \'post_status\' => \'inherit\', 
        \'post_mime_type\' => \'image\', 
        \'post_parent\' => $id, 
        \'paged\' => $gallery_page,
        \'post__in\'    => $post__in,
        \'post__not_in\'    => $post__not_in
    ); 
    $gallery = new WP_Query($args);

        if ( $gallery->have_posts() ) :
            $output .= apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );
                    while ( $gallery->have_posts() ) : $gallery->the_post();
                        $output .= wp_get_attachment_image( $post->ID, $size );
                    endwhile;
            $output .= \'</div>\';
            $output .= $gallery_nav_div;
                    if ( get_option(\'permalink_structure\') ) {
                        $format = \'gallery/image/%#%\';
                    } else {
                        $format = \'&gallery_page=%#%\';
                    }
                    $args = array(
                        \'base\' => get_permalink( $post->post_parent ) . \'%_%\',
                        \'format\' => $format,
                        \'current\' => $gallery_page,
                        \'total\' => $gallery->max_num_pages
                    );
                    $output .= paginate_links( $args );
                    wp_reset_postdata();
            $output .= \'</div>\';
        endif;
    return $output;
}

4。其他代码请查看How to paginate attachments in a secondary query as gallery? 有关说明,请添加查询变量

Code:

add_filter(\'init\', \'wpse89462_attachment_gallery_add_query_var\');
function wpse89462_attachment_gallery_add_query_var() {
    global $wp;
    $wp->add_query_var(\'gallery_page\');
}
重写

Code:

add_filter(\'init\', \'wpse89462_attachment_gallery_add_rewrite_tag_rule\');
function wpse89462_attachment_gallery_add_rewrite_tag_rule() {
    add_rewrite_tag(\'%gallery_page%\',\'([^&]+)\');
    // rewrite rules have to be added according to needs
    // below two rules are for two specitic cases
    // /{year}/{month}/{name}/[gallery_page rewrite]
    add_rewrite_rule(\'([0-9]{4})/([0-9]{2})/([^/]+)?/gallery/image//?([0-9]{1,})/?$\', \'index.php?year=$matches[1]&month=$matches[2]&name=$matches[3]&gallery_page=$matches[4]\', \'top\');
    // /{name}/[gallery_page rewrite]
    add_rewrite_rule(\'([^/]+)/gallery/image//?([0-9]{1,})/?$\', \'index.php?name=$matches[1]&gallery_page=$matches[2]\', \'top\');
}

结束

相关推荐

Remove duplicate attachments

情况是这样的:我有一个自动脚本,可以上传附件并将每个附件链接到特定的帖子。由于错误,脚本运行了多次,我有以下内容媒体库中针对单个文件的多个附件帖子(不同的附件帖子具有相同的文件URL)。这些附件中有一个实际上是附在帖子上的。我想做的显然是清理媒体库。我需要在不删除文件的情况下删除附件帖子,并且确保我不删除那些实际附加到他们帖子的帖子。有什么想法吗?