$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Add pagination to for each taxonomy terms|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Add pagination to for each taxonomy terms

matteradmin8PV0评论

I am tring to add a line of code that paginate the taxonomies to serveral pages this lin is a function i created called

wpbeginner_numeric_posts_nav();

but every time i add the function after the foreach close paracket the pagination created but all the pages have the same taxonomy in page 1

the code that i use is this

<?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 { ?>

I am tring to add a line of code that paginate the taxonomies to serveral pages this lin is a function i created called

wpbeginner_numeric_posts_nav();

but every time i add the function after the foreach close paracket the pagination created but all the pages have the same taxonomy in page 1

the code that i use is this

<?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 { ?>
Share Improve this question edited Feb 8, 2019 at 11:21 Alexander Holsgrove 1,9091 gold badge15 silver badges25 bronze badges asked Feb 8, 2019 at 10:35 Nader ElsayedNader Elsayed 1114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

That pagination is for posts, whereas you are trying to paginate terms.

One approach is to create the pagination for yourself:

$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);

Untested, but that's a starting point. You can use $total_pages and $current_page to build the pagination links.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far