为每个分类术语添加分页

时间:2019-02-08 作者:Nader Elsayed

我想添加一行代码,将分类法分页到多个页面。这个lin是我创建的一个函数,名为

wp初学者\\u numeric\\u posts\\u nav();

但每次我在foreach close paracket之后添加函数时,都会创建分页,但所有页面在第1页中都具有相同的分类法

我使用的代码是

<?php get_header(\'category\');?>

 <section class="gallery-block compact-gallery">
        <div class="container">
        <div class="row mx-auto justify-content-center  ">
                 <?php
     $term = get_queried_object();
    //  if($term->post_parent !=0 ){
        $term_id = $term->term_id;
        $taxonomy_name = $term->taxonomy;
        $termchildren = get_term_children( $term_id, $taxonomy_name );
         // echo    $postcat ;
         if ( !empty( $termchildren )){
         foreach ($termchildren  as $child) {
        $term = get_term_by( \'id\', $child, $taxonomy_name );
                 ?>
                 <div class="col-md-6 col-lg-3 item bg-white mb-5 p-0 mr-2">
                     <a class="lightbox" href="<?php  echo get_term_link( $term, $taxonomy_name );?>">
                     <img class="img-fluid image w-100 h-50" src=" <?php echo get_theme_file_uri(\'/images/\'.$term->name.\'.png\');?>">
                             <span class="description">
                             <span class="description-heading tajawal "><?php echo $term->name ;?></span>
                             <span class="description-body"></span>
                                 </span>
                                 </a>
                 </div>


                 <?php } wpbeginner_numeric_posts_nav(); }else { ?>

1 个回复
SO网友:Alexander Holsgrove

分页是针对帖子的,而您正试图对术语进行分页。

一种方法是为自己创建分页:

$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children($term_id, $taxonomy_name);

$per_page = 10;
$page  = get_query_var(\'page\');
$paged = get_query_var(\'paged\');
$total_pages = ceil(count($termchildren) / $per_page);

$current_page = ($paged ? $paged : ($page ? $page : 1));
$offset = (($current_page - 1) * $per_page);

$term_children = array_slice($term_children, $offset, $per_page);
未经测试,但这是一个起点。您可以使用$total_pages$current_page 构建分页链接。

相关推荐

Custom pagination structure

当前我的分页工作方式如下http://example.com/city/usa/ #for page 1 http://example.com/city/usa/page/2 http://example.com/city/usa/page/3 ...and so on 我想将其更改为:http://example.com/city/usa/ #for page 1 http:/