如何在wordpress中创建分页

时间:2014-07-21 10:41:42

标签: wordpress pagination

我正在使用wordpress并希望为帖子添加分页。代码是好的机器人没有工作在chrome.When我点击分页它进入当前页面,如果我点击下一个和上一个链接它转到主页URL但在mozila和其他浏览器它工作正常。我的代码是

<?php 
if ( !function_exists( 'appcara_pagination' ) ) {

    function appcara_pagination($total) {

        $prev_arrow = is_rtl() ? 'next' : 'prev';
        $next_arrow = is_rtl() ? 'prev' : 'next';

        global $wp_query;
         //$total = $wp_query->max_num_pages;
         //$total="10";
        $big = 999999999; // need an unlikely integer
        if( $total > 1 )  {
             if( !$current_page = get_query_var('paged') )
                 $current_page = 1;
             if( get_option('permalink_structure') ) {
                 $format = '/%#%/';
             } else {
                 $format = '&paged=%#%';
             }
            echo paginate_links(array(
                'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format'        => $format,
                'current'       => max( 1, get_query_var('paged') ),
                'total'         => $total,
                'mid_size'      => 3,
                'type'          => 'list',
                'prev_text'     => $prev_arrow,
                'next_text'     => $next_arrow,
             ) );
        }
    }

}
function appcara_paginate_links( $args = '' ) {
    $defaults = array(
        'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
        'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
        'total' => 1,
        'current' => 0,
        'show_all' => false,
        'prev_next' => true,
        'prev_text' => __('&laquo; Previous'),
        'next_text' => __('Next &raquo;'),
        'end_size' => 1,
        'mid_size' => 2,
        'type' => 'plain',
        'add_args' => false, // array of query args to add
        'add_fragment' => '',
        'before_page_number' => '',
        'after_page_number' => ''
    );

    $args = wp_parse_args( $args, $defaults );
    extract($args, EXTR_SKIP);

    // Who knows what else people pass in $args
    $total = (int) $total;
    if ( $total < 2 )
        return;
    $current  = (int) $current;
    $end_size = 0  < (int) $end_size ? (int) $end_size : 1; // Out of bounds?  Make it the default.
    $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
    $add_args = is_array($add_args) ? $add_args : false;
    $r = '';
    $page_links = array();
    $n = 0;
    $dots = false;
        $prev_link = '<p class="prev-div" ><span class="prev-click"></span></p>';
    if ( $prev_next && $current && 1 < $current ) :
        $link = str_replace('%_%', 2 == $current ? '' : $format, $base);
        $link = str_replace('%#%', $current - 1, $link);
        if ( $add_args )
            $link = add_query_arg( $add_args, $link );
        $link .= $add_fragment;

        /**
         * Filter the paginated links for the given archive pages.
         *
         * @since 3.0.0
         *
         * @param string $link The paginated link URL.
         */
        $prev_link = '<a class="prev-div" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '"><span class="prev-click"></span></a>';
    endif;
    for ( $n = 1; $n <= $total; $n++ ) :
        if ( $n == $current ) :
            $page_links[] = "<h3 class='page-numbers'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</h3>";
            $dots = true;
        else :
            if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
                $link = str_replace('%_%', 1 == $n ? '' : $format, $base);
                $link = str_replace('%#%', $n, $link);
                if ( $add_args )
                    $link = add_query_arg( $add_args, $link );
                $link .= $add_fragment;

                /** This filter is documented in wp-includes/general-template.php */
                $page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</a>";
                $dots = true;
            elseif ( $dots && !$show_all ) :
                $page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
                $dots = false;
            endif;
        endif;
    endfor;
        $next_link = '<p class="next-div" ><span class="next-click"></span></p>';
    if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
        $link = str_replace('%_%', $format, $base);
        $link = str_replace('%#%', $current + 1, $link);
        if ( $add_args )
            $link = add_query_arg( $add_args, $link );
        $link .= $add_fragment;

        /** This filter is documented in wp-includes/general-template.php */
        //$page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
                $next_link = '<a class="next-div" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '"><span class="next-click"></span></a>';
    endif;

    switch ( $type ) :
        case 'array' :
            return $page_links;
            break;
        case 'list' :
                        $r.='<div class="pagination">';
                        $r.=$prev_link;
            $r .= "<ul class='page-numbers'>\n\t<li>";
            $r .= join("</li>\n\t<li>", $page_links);
            $r .= "</li>\n</ul>\n";
                        $r.=$next_link;
                        $r.='<div class="clear"></div></div>';
            break;
        default :
            $r = join("\n", $page_links);
            break;
    endswitch;
    return $r;
}
?>


calling this function 

<?php
//WordPress loop for custom post type
    $url=explode('/',$_SERVER['REQUEST_URI']);
 $paged = (get_query_var('page')) ? get_query_var('page') : 1;
 $my_query = new WP_Query('post_type=blogs&posts_per_page=10&paged='.$url['4']);
 $total=wp_count_posts('blogs');
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <div id="blogs">
        <div class="content_blog custom_post" >
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <i><?php echo get_the_date('', $post->ID).'-'.get_the_time('', $post->ID); ?></i>
        <?php the_excerpt();
        $term_list = wp_get_post_terms($post->ID, 'blog-cat');
        echo "categories:-";
        foreach($term_list as $key=>$cat)
        {

            echo "<a href='".get_term_link(intval($cat->term_id),'blog-cat')."'>".$cat->name.'</a>,';
        }
        ?>
        <?php  ?>
        </div>

        </div>

<?php endwhile;  wp_reset_query(); ?>
<?php appcara_pagination($total->publish/10);

 ?>
<style>
    .pagination ul { list-style: none; border-left: 1px solid #d9d6d6; border-right: 1px solid #d9d6d6; float: left; width: 88%; }
.pagination ul li { display: inline-block; }
.pagination ul li a { display: block; font-size: 16px; font-weight: 600; color: #515050; padding: 6px 2px; }
.prev-div, .next-div { text-align: center; float: left; width: 68px; }
.prev-click, .next-click { display: inline-block; width: 10px; height: 16px; margin: 9px 0 0; }
.prev-click { background-position: -280px -311px; }
.next-click { background-position: -280px -331px; }
.prev-click:hover, .prev-div:hover .prev-click { background-position: -298px -311px; }
.next-click:hover { background-position: -301px -331px; }
.pagination ul li a:hover { color: #d75c43; }


</style>

1 个答案:

答案 0 :(得分:0)

您可以将此代码用于functions.php文件,它正常工作..

/*************************************
*  Wordpress pagination 
**************************************/

function pagination($pages = '', $range = 3)
    { 
        $showitems = ($range * 3)+1;
         global $paged; if(empty($paged)) $paged = 1;
         if($pages == '') {
         global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages)
         { $pages = 1; } }
         if(1 != $pages)
         { echo "<div class='navigation'>";?>
         <?php      previous_posts_link(); 
         echo "<div class='pagination'><ul>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a rel='nofollow' href='".get_pagenum_link(1)."'>&laquo; First</a></li>";
         if($paged > 1 && $showitems < $pages) echo "<li><a rel='nofollow' href='".get_pagenum_link($paged - 1)."' class='inactive'>&lsaquo; Previous</a></li>";
         for ($i=1; $i <= $pages; $i++)
         { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         { echo ($paged == $i)? "<li class='current'><span class='currenttext'>".$i."</span></li>":"<li><a rel='nofollow' href='".get_pagenum_link($i)."' class='inactive'>".$i."</a></li>";
         } } if ($paged < $pages && $showitems < $pages) echo "<li><a rel='nofollow' href='".get_pagenum_link($paged + 1)."' class='inactive'>Next &rsaquo;</a></li>";
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a rel='nofollow' class='inactive' href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
         echo "</ul></div>";?>
        <?php next_posts_link();
        echo "</div>"; }
    }

在索引文件中使用:

<?php pagination();?>