用“丑陋的”和“漂亮的”永久链接对链接进行分页?

时间:2014-12-20 作者:Dejo Dekic

如果我启用了“pretty”永久链接,我的代码将适用于静态页面(如果页面设置为“主页”或“普通页面”,那么代码将适用)。但如果我使用“default permalink”结构,代码就会中断。

到目前为止,我有这样的想法(与“漂亮的permalinks”配合得很好,但与“丑陋的permalinks”配合得很好):

/*Declared above the loop*/
    if ( get_query_var( \'paged\' ) ) {
    $paged = get_query_var( \'paged\' );
    } elseif ( get_query_var( \'page\' ) ) {
    $paged = get_query_var( \'page\' );
    } else {
    $paged = 1;
    }

    /*The loop and arguments go here*/

    global $wp_query;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, $paged ),
    \'total\' => $wp_query->max_num_pages,
    \'prev_text\' => esc_html__( \'Prev\', \'domain\' ),
    \'next_text\' => esc_html__( \'Next\', \'domain\' ),
    \'end_size\'  => 1,
    \'mid_size\'  => 1,
    ) );
是否可以对这两个(丑陋的永久链接和美丽的永久链接)进行编号分页?

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

编辑-WORDPRESS 4.4.1错误请注意,WORDPRESS 4.4.1中有一个错误(我不确定这个错误是否也会影响WORDPRESS 4.4,但根据错误报告,它不会影响WORDPRESS 4.4),当将页面设置为静态首页时,分页失败。请务必查看错误报告(trac ticket #35344])和补丁。根据错误报告,这将在WordPress 4.4.2中修复。

快乐B/天更新/编辑

我一年前最初编写的代码只接受了完整的WP_Query 我意识到查询对象在执行this answer您也可以签出该函数),该函数还需要接受query 以便同时分页其他类型的查询

我已经更新了原始代码,所以现在您只需将任何整数/数值传递给query.

原来的答案实际上我昨天刚刚完成了一个供我个人使用的函数,因为我需要一些更灵活的东西来处理所有查询。我决定,如果有超过5页的内容,我需要至少5页的页码才能始终可见。此外,我还需要一个指向第一页和最后一页的链接

此外,我还需要在编号页码和普通分页链接之间灵活切换。此外,该功能必须灵活,以便即使在静态头版上也可以使用。

我不打算对函数的操作和内部工作做太多的介绍,因为我已经对函数中最重要的几点进行了注释。

功能:

/**
 * @author Pieter Goosen
 * @license GPLv2 
 * @link http://www.gnu.org/licenses/gpl-2.0.html
 *
 * This function returns numbered pagination links or text pagination links
 * depending what is been set
 *
 * Paginated numbered links uses get_pagenum_link() to return links to the
 * required pages
 * @uses http://wpseek.com/function/get_pagenum_link/
 *
 * The pagination links uses next_posts_link() and previous_posts_link()
 * @uses http://codex.wordpress.org/Function_Reference/next_posts_link
 * @uses http://codex.wordpress.org/Template_Tags/previous_posts_link
 *
 * @param array $args An array of key => value arguments. Defaults below 
 * - mixed query variable                   \'query\'                 => $GLOBALS[\'wp_query\'],
 * - string Previous page text              \'previous_page_text\'    => __( \'«\' ),
 * - string Next page text                  \'next_page_text\'        => __( \'»\' ),
 * - string First page link text            \'first_page_text\'       => __( \'First\' ),
 * - string Last page link text             \'last_page_text\'        => __( \'Last\' ),
 * - string Older posts text                \'next_link_text\'        => __( \'Older Entries\' ),
 * - string Newer posts text                \'previous_link_text\'    => __( \'Newer Entries\' ),
 * - bool Whether to use links              \'show_posts_links\'      => false,
 * - int Amount of numbered links to show   \'range\'                 => 5,
 *
 * @return string $paginated_text
*/ 
function get_paginated_numbers( $args = [] ) {

    //Set defaults to use
    $defaults = [
        \'query\'                 => $GLOBALS[\'wp_query\'],
        \'previous_page_text\'    => __( \'«\' ),
        \'next_page_text\'        => __( \'»\' ),
        \'first_page_text\'       => __( \'First\' ),
        \'last_page_text\'        => __( \'Last\' ),
        \'next_link_text\'        => __( \'Older Entries\' ),
        \'previous_link_text\'    => __( \'Newer Entries\' ),
        \'show_posts_links\'      => false,
        \'range\'                 => 5,
    ];

    // Merge default arguments with user set arguments
    $args = wp_parse_args( $args, $defaults );

    /**
     * Get current page if query is paginated and more than one page exists
     * The first page is set to 1
     * 
     * Static front pages is included
     *
     * @see WP_Query pagination parameter \'paged\'
     * @link http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
     *
    */ 
    if ( get_query_var(\'paged\') ) { 

        $current_page = get_query_var(\'paged\'); 

    }elseif ( get_query_var(\'page\') ) { 

        $current_page = get_query_var(\'page\'); 

    }else{ 

        $current_page = 1; 

    }

    // Get the amount of pages from the query
    if ( is_object( $args[\'query\'] ) ) {
        $max_pages = (int) $args[\'query\']->max_num_pages;
    } else {
        $max_pages = filter_var( $args[\'query\'], FILTER_VALIDATE_INT );
    }

    /**
     * If $args[\'show_posts_links\'] is set to false, numbered paginated links are returned
     * If $args[\'show_posts_links\'] is set to true, pagination links are returned
    */
    if( false === $args[\'show_posts_links\'] ) {

        // Don\'t display links if only one page exists
        if( 1 === $max_pages ) {

            $paginated_text = \'\';

        }else{

            /**
             * For multi-paged queries, we need to set the variable ranges which will be used to check
             * the current page against and according to that set the correct output for the paginated numbers
            */
            $mid_range      = (int) floor( $args[\'range\'] / 2 );
            $start_range    = range( 1 , $mid_range );
            $end_range      = range( ( $max_pages - $mid_range +1 ) , $max_pages );
            $exclude        = array_merge( $start_range, $end_range );  

            /**
             * The amount of pages must now be checked against $args[\'range\']. If the total amount of pages
             * is less than $args[\'range\'], the numbered links must be returned as is
             *
             * If the total amount of pages is more than $args[\'range\'], then we need to calculate the offset
             * to just return the amount of page numbers specified in $args[\'range\']. This defaults to 5, so at any
             * given instance, there will be 5 page numbers displayed
            */
            $check_range    = ( $args[\'range\'] > $max_pages )   ? true : false;

            if( true === $check_range ) {

                $range_numbers = range( 1, $max_pages );

            }elseif( false === $check_range ) {

                if( !in_array( $current_page, $exclude ) ) {

                    $range_numbers = range( ( $current_page - $mid_range ), ( $current_page + $mid_range ) );

                }elseif( in_array( $current_page, $start_range ) && ( $current_page - $mid_range ) <= 0 ) {

                    $range_numbers = range( 1, $args[\'range\'] );

                }elseif(  in_array( $current_page, $end_range ) && ( $current_page + $mid_range ) >= $max_pages ) {

                    $range_numbers = range( ( $max_pages - $args[\'range\'] +1 ), $max_pages );

                }

            }

            /**
             * The page numbers are set into an array through this foreach loop. The current page, or active page
             * gets the class \'current\' assigned to it. All the other pages get the class \'inactive\' assigned to it
            */
            foreach ( $range_numbers as $v ) {

                if ( $v == $current_page ) { 

                    $page_numbers[] = \'<span class="current">\' . $v . \'</span>\';

                }else{

                    $page_numbers[] = \'<a href="\' . get_pagenum_link( $v ) . \'" class="inactive">\' . $v . \'</a>\';

                }

            }

            /** 
            * All the texts are set here and when they should be displayed which will link back to:
             * - $previous_page The previous page from the current active page
             * - $next_page The next page from the current active page
             * - $first_page Links back to page number 1
             * - $last_page Links to the last page
            */
            $previous_page  = ( $current_page !== 1 )                       ? \'<a href="\' . get_pagenum_link( $current_page - 1 ) . \'">\' . $args[\'previous_page_text\'] . \'</a>\' : \'\';
            $next_page      = ( $current_page !== $max_pages )              ? \'<a href="\' . get_pagenum_link( $current_page + 1 ) . \'">\' . $args[\'next_page_text\'] . \'</a>\'     : \'\';
            $first_page     = ( !in_array( 1, $range_numbers ) )            ? \'<a href="\' . get_pagenum_link( 1 ) . \'">\' . $args[\'first_page_text\'] . \'</a>\'                    : \'\';
            $last_page      = ( !in_array( $max_pages, $range_numbers ) )   ? \'<a href="\' . get_pagenum_link( $max_pages ) . \'">\' . $args[\'last_page_text\'] . \'</a>\'            : \'\';

            /**
             * Text to display before the page numbers
             * This is set to the following structure:
             * - Page X of Y
            */
            $page_text      = \'<span>\' . sprintf( __( \'Page %s of %s\' ), $current_page, $max_pages ) . \'</span>\';
            // Turn the array of page numbers into a string
            $numbers_string = implode( \' \', $page_numbers );

            // The final output of the function
            $paginated_text = \'<div class="pagination">\';
            $paginated_text .= $page_text . $first_page . $previous_page . $numbers_string . $next_page . $last_page;
            $paginated_text .= \'</div>\';

        }

    }elseif( true === $args[\'show_posts_links\'] ) {

        /**
        * If $args[\'show_posts_links\'] is set to true, only links to the previous and next pages are displayed
        * The $max_pages parameter is already set by the function to accommodate custom queries
        */
        $paginated_text = get_next_posts_link( \'<div class="next-posts-link">\' . $args[\'next_link_text\'] . \'</div>\', $max_pages );
        $paginated_text .= get_previous_posts_link( \'<div class="previous-posts-link">\' . $args[\'previous_link_text\'] . \'</div>\' );

    }

    // Finally return the output text from the function
    return $paginated_text;

}
用法也对可用的参数进行了很好的注释。如果需要显示自定义查询的编号链接,请参见下面的示例$q = new WP_Query

$args = [
    \'query\' => $q
];
echo get_paginated_numbers( $args );
如果需要,您也可以简单地将自定义文本传递给函数。

如果需要显示链接而不是页码,请尝试以下操作

$args = [
    \'query\'            => $q
    \'show_posts_links\' => true,
];
echo get_paginated_numbers( $args );
使用您自己的样式,您可以获得如下所示的页码

enter image description here

SO网友:Dejo Dekic

因此,看起来有可能使此函数与“丑陋”和“美丽”永久链接一起工作。代码如下:

  <?php
    global $wp_query;

    //structure of "format" depends on whether we\'re using pretty permalinks
    if( get_option(\'permalink_structure\') ) {
    $format = \'?paged=%#%\';
    } else {
    $format = \'page/%#%/\';
    } 
    $big = 999999999; // need an unlikely integer
    $base = $format ==\'?paged=%#%\' ? $base = str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ) : $base = @add_query_arg(\'paged\',\'%#%\');

    echo paginate_links( array(
    \'base\' => $base,
    \'format\' => $format,
    \'current\' => max( 1, $paged ),
    \'total\' => $wp_query->max_num_pages,
    \'prev_text\' => esc_html__( \'Prev\', \'domain\' ),
    \'next_text\' => esc_html__( \'Next\', \'domain\' ),
    \'end_size\'  => 1,
    \'mid_size\'  => 1,
    ) );
 ?>
希望它能帮助别人;)

结束

相关推荐

Pagination and Related Posts

是否可以对嵌入在single中的以下相关POST代码使用分页。我的主题的php。 <?php // for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $