如何更改分页的输出显示?

时间:2018-10-24 作者:Peter

如何更改分页的输出显示?在过去的几个小时里,我一直在尝试将分页链接(1、2、3等)的输出格式化为如下所示:

Pagination Screenshot - GOAL

我无法修改代码,使其更接近于此。有人能告诉我该怎么做吗?

这是我的代码:

<?php
    if ( get_query_var( \'paged\' ) ) {
        $paged = get_query_var(\'paged\');
    }elseif( get_query_var( \'page\' ) ) {
        $paged = get_query_var( \'page\' );
    }else{
        $paged = 1;
    }

    $per_page = 3;
    $number_of_terms = wp_count_terms( \'series\' ); // This counts the total number terms in the taxonomy with a function)
    $paged_offset = ($paged - 1) * $per_page;

    $args = array(
        \'orderby\'           => \'ID\',
        \'order\'             => \'DESC\',
        \'hide_empty\'        => 0,
        \'number\'            => $per_page,
        \'offset\'            => $paged_offset
    );  

    $terms = get_terms(\'series\', $args);  

    foreach($terms as $term){ ?>

        <div class="block_item article">
            <div  class="article_image" style="background: url(\'<?php the_field(\'series_artwork\', $term); ?>\'); background-size: cover; background-position: 50%;"></div>
            <h4 class="section_label"><?php the_field(\'date\', $term); ?></h4>
            <div class="block_item_content">
                <h3><?php echo $term->name; ?></h3>
                <p><?php echo $term->description; ?></p>
                <a href="<?php echo get_term_link($term->slug, \'series\'); ?>" class="button_styling">
                    Read More
                </a>
            </div>
        </div>

    <?php } 

    $big = 999999999; // need an unlikely integer
    echo paginate_links( 
        array(
            \'before_page_number\' => \'<div class="pagination"><span>Page \'. $paged .\' of \' . $term->max_num_pages . \'</span></div>\',
            \'base\'    => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\'  => \'/page/%#%\',
            \'current\' => max( 1, get_query_var(\'paged\') ),
            \'total\'   => ceil( $number_of_terms / $per_page ),
            \'prev_text\' => __(\'\'),
            \'next_text\' => __(\'\')
        ) 
    );
?>
如果你能让它与我现有的功能(我一直在尝试做的事情)一起工作,那么就给你加分!

functions.php

// numbered pagination
function pagination($pages = \'\', $range = 4)
{  
 $showitems = ($range * 2)+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=\\"pagination\\"><span>Page ".$paged." of ".$pages."</span>";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>&laquo; First</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo; Previous</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href=\\"".get_pagenum_link($paged + 1)."\\">Next &rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last &raquo;</a>";
     echo "</div>\\n";
 }
}

Function Call

<?php if (function_exists("pagination")) {
     pagination($terms->max_num_pages);
} ?>
感谢您提供的任何帮助!

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

下面将循环使用自定义分类法并提取所有相关术语。

<?php if ( get_query_var( \'paged\' ) ) {
        $page = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
    }

    $per_page = 9;
    $totalterms   = wp_count_terms( \'series\' ); 
    $totalpages   = ceil( $totalterms / $per_page );
    $paged_offset = ( $page > 0 ) ?  $per_page * ( $page - 1 ) : 1;

    $args = array(
        \'orderby\'       => \'ID\', 
        \'order\'         => \'DSC\',
        \'hide_empty\'    => true, 
        \'exclude\'       => array(), 
        \'exclude_tree\'  => array(), 
        \'include\'       => array(),
        \'number\'        => $per_page, 
        \'fields\'        => \'all\', 
        \'slug\'          => \'\', 
        \'parent\'         => \'\',
        \'hierarchical\'  => true, 
        \'child_of\'      => 0, 
        \'get\'           => \'\', 
        \'name__like\'    => \'\',
        \'pad_counts\'    => false, 
        \'offset\'        => $paged_offset, 
        \'search\'        => \'\', 
        \'cache_domain\'  => \'core\'
    );

    $terms = get_terms(\'series\', $args);  

    foreach($terms as $term){ ?>

        <div class="block_item article">
            <div class="block_item_overlay"></div>
            <div  class="article_image" style="background: url(\'<?php the_field(\'series_artwork\', $term); ?>\'); background-size: cover; background-position: 50%;"></div>
            <h4 class="section_label"><?php the_field(\'date\', $term); ?></h4>
            <div class="block_item_content">
                <h3><?php echo $term->name; ?></h3>
                <p><?php echo $term->description; ?></p>
                <a href="<?php echo get_term_link($term->slug, \'series\'); ?>" class="button_styling">
                    Read More
                </a>
            </div>
        </div>

    <?php } 

    $big = 999999999; // need an unlikely integer
    printf( \'<div class="pagination"><span>Page \'.$page.\' of \'.$totalpages.\'</span>\', 
        custom_page_navi( $totalpages, $page, 3, 0 ) 
    );

    echo paginate_links( 
        array(
            \'base\'    => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\'  => \'/page/%#%\',
            \'current\' => max( 1, get_query_var(\'paged\') ),
            \'total\'   => ceil( $totalterms / $per_page ),
            \'prev_next\' => false
        ) 
    ); ?>
    </div>

SO网友:Sally CJ

我可以看出,您正在努力实现以下输出:

<div class="pagination">
  <span>Page {current} of {total pages}</span>
  {links here}
</div>
你这样尝试过:

\'before_page_number\' => \'<div class="pagination"><span>Page \'. // wrapped for clarity
  $paged .\' of \' . $term->max_num_pages . \'</span></div>\'
但这实际上会在生成的链接中的每个页码之前添加标记。(以及$term->max_num_pages 也是null 因为在这种情况下,$term 没有max_num_pages 属性。)

下面是您如何实现预期输出的方法:

$big = 999999999; // need an unlikely integer
$cur_page = max( 1, $paged );
$num_pages = ceil( $number_of_terms / $per_page );
$links = paginate_links(
    array(
        \'base\'    => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\'  => \'/page/%#%\',
        \'current\' => $cur_page,
        \'total\'   => $num_pages,
        \'prev_text\' => __(\'\'),
        \'next_text\' => __(\'\')
    )
);

// If there are links to paginate, display the pagination.
if ( $links ) {
    $before = \'<span>Page \'. $cur_page .\' of \' . $num_pages . \'</span>\';
    echo \'<div class="pagination">\' . $before . \' \' . $links . \'</div>\';
}
请参见paginate_links() reference 有关该函数的更多详细信息,例如mid_sizeend_size 控制要显示的页面/链接数的参数。

结束

相关推荐

什么时候应该/不应该使用wp_get_post_Terms与Get_the_Terms?

一位用户在codex中提供了一条注释,指出唯一的区别是get\\u术语使用缓存的数据,我已经知道wp\\u get\\u post\\u术语可以让您通过slug检索数据,但我仍然想知道使用其中一个与另一个的最佳情况是什么。