0

I'm trying to list a taxonomy in alphabetical order. I've found west code to do it but it gives throws an error: Warning: Division by zero

functions.php

if( !function_exists('a_z_pagination') ){
function a_z_pagination() {
$order = isset( $_REQUEST['sortby'] ) ?  trim($_REQUEST['sortby']) : null;
$sort_array = array(
'A' =>  __('A','framework'),
'B' =>  __('B','framework'),
'C' =>  __('C','framework'),
'D' =>  __('D','framework'),
'E' =>  __('E','framework'),
'F' =>  __('F','framework'),
'G' =>  __('G','framework'),
'H' =>  __('H','framework'),
'I'    =>   __('I','framework'),
'J'    =>   __('J','framework'),
'K' =>  __('K','framework'),
'L' =>  __('L','framework'),
'M' =>  __('M','framework'),
'N' =>  __('N','framework'),
'O' =>  __('O','framework'),
'P' =>  __('P','framework'),
'Q' =>  __('Q','framework'),
'R' =>  __('R','framework'),
'S' =>  __('S','framework'),
'T' =>  __('T','framework'),
'U' =>  __('U','framework'),
'V' =>  __('V','framework'),
'W' =>  __('W','framework'),
'X' =>  __('X','framework'),
'Y' =>  __('Y','framework'),
'Z' =>  __('Z','framework')
);
$block = '';
$block .= '<div class="paginator lighter"><ul>';
foreach ( $sort_array as $key=>$value ){
$active = ( $order == $key ) ? 'selected' : null;
$block .= '<li><a class="'.$active.'"  href="'.get_permalink().'?sortby='.$key.'">'.$value.'</a></li>';     
}
$block .= '</ul></div>';
print $block; 
}
add_action('a_z_pagination', 'a_z_pagination');
}

page Error in line: 'total' => ceil($total_terms / $per_page )

<?php do_action('a_z_pagination'); // A Z PAGINATION IS HERE!  ?>

<div class="content">
<?php
$per_page = '1';
$sortby = isset($_GET['sortby']) ? $_GET['sortby'] : null;
$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
// number of tags to show per-page
$per_page = $number_of_performers ;
$offset = ( $page -1 ) * $per_page;
$args = array( 'orderby' => $orderby,  'number' => $per_page,  
    'offset' => $offset,
    'exclude'       => array(), 
    'exclude_tree'  => array(), 
    'include'       => array(),
    'fields'        => 'all', 
    'hierarchical'  => true, 
    'child_of'      => 0, 
    'hide_empty' => 0,
    'pad_counts'    => false,
    //'terms'     =>  $term->slug,
    'cache_domain'  => 'core' );
$taxonomy = 'autor';
$count_performers = wp_count_terms( $taxonomy, $offset );
$tax_terms = get_terms( $taxonomy, $args );
$total_terms = wp_count_terms( $taxonomy, $offset );
?>
<?php foreach ($tax_terms as $tag) : ?>
<?php 
$flag = 0; 
if( $sortby == substr( $tag->name, 0, 1 ) || $sortby=='' ) {    $flag = 1;} 
if ($flag=='1'){ // BELOW IS THE LAST POST IMAGE FROM TAX
?>
<div class="thumbs">

<a href="<?php echo get_term_link($tag->slug, 'autor'); ?>" title="<?php echo $tag->name; ?>">  

<?php $args = 
 array ('showposts' => 1,'orderby' => 'rand','order' => 'DESC','tax_query' => array(
      array('taxonomy' => 'autor','field'    => 'term_id','terms'    => $tag->term_id,),
   ),
);
$new_query = new WP_Query( $args ); if ( $new_query->have_posts() ) { while ( $new_query->have_posts() ) { $new_query->the_post();  $post_id = get_the_ID();
get_template_part( 'templates/image', get_post_format() ); } 
} else {
} 
wp_reset_postdata(); 
?>

</a>

<p><?php echo $tag->name; ?></p>
<p><?php echo $tag->count;  _e('libros', 'framework'); ?></p>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
<div class="clear"></div>
<?php
echo '<div class="paginator lighter">'; // NUMERIC PAGINATION IS HERE!
   $page_next = 'NEXT PAGE'; $page_prev = 'PREVIOUS PAGE';
   $big = 999999999; // need an unlikely integer
   if ( wp_is_mobile() ) { $ranger = $mid_size; } else { $ranger = '3'; }
   echo paginate_links( 
   array(
       'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
       'format'  => '/page/%#%',
        'current'       => max( 1, $paged ),
        'prev_text'     => $page_prev,
        'next_text'     => $page_next,
        'show_all'      => false,
        'mid_size' => $ranger,
        'before_page_number' => '',
        'type' => 'list',
          'after_page_number' => '',
        'total'   => ceil($total_terms / $per_page )  // Error:  Warning: Division by zero
    ) 
 );
echo '</div>';
get_footer(); ?>
  • 1
    where do you set $number_of_performers? – Milo Dec 1 '17 at 17:39
  • Clearly either $total_terms or $per_page is zero, can you make your code more readable by indenting correctly, removing unnecessary closing an open tags, and making sure there is 1 thing per line rather than multiple things per line? Your editor should automatically do a lot of this for you as you write code, consider a better editor such as Atom, Sublime Text, or PHPStorm, the first 2 of which can be used for free – Tom J Nowell Dec 1 '17 at 18:33
1

$per_page is $number_of_performers, which must be an incorrect value.

PS: Also you can reduce your $sort_array defines with:

$sort_array = array();
$alphas = range('A', 'Z');
foreach ($alphas as $lttr)
   $sort_array[$lttr] = __($lttr,'framework');

UPDATE

furthering @tom-j-nowell point of $total_terms being the problem - your codes wp_count_terms()'s second argument is supposed to be an array or a string query, not an integer like $offset is.

While troubleshooting, it may benefit to see the math and variable values so you can get a better idea what is the problem. Above your .paginator.lighter echo, I'd add:

echo "<pre>
total_terms = {$total_terms}\n
per_page = {$per_page}\n
---
total = ".($total_terms/$per_page)."</pre>";
  • thanks for your help. I have solved the error by eliminating the lines: – Joel Dec 1 '17 at 20:08
  • I have solved the error by eliminating the lines: $per_page = $number_of_performers ; $count_performers = wp_count_terms( $taxonomy, $offset ); Now I have another problem. $ per_page = '3'; On page 1 only shows one result and in the rest 3. – Joel Dec 1 '17 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.