0

I am trying to paginate taxonomy cities, with custom post type city, the categories that are displayed but failed to understand how can do it with the wp_custom_pagination function.

function wp_custom_pagination($before = '', $after = '') {
        global $wpdb, $wp_query,$wp_rewrite;
        $wp_rewrite->flush_rules();
        $custom_pagination = array();
        $custom_pagination['pages_text'] = ('');
        $custom_pagination['current_text'] = '%PAGE_NUMBER%';
        $custom_pagination['page_text'] = '%PAGE_NUMBER%';
        $custom_pagination['first_text'] = ('First Page');
        $custom_pagination['last_text'] = ('Last Page');
        $custom_pagination['next_text'] = 'Next';
        $custom_pagination['prev_text'] = 'Prev';
        $custom_pagination['dotright_text'] = '...';
        $custom_pagination['dotleft_text'] = '...';
        $custom_pagination['num_pages'] = 3; //continuous block of page numbers
        $custom_pagination['always_show'] = 0;
        $custom_pagination['num_larger_page_numbers'] = 0;
        $custom_pagination['larger_page_numbers_multiple'] = 1;

        //If NOT a single Post is being displayed
        /*http://codex.wordpress.org/Function_Reference/is_single)*/
        if (!is_single()) {
                $request = $wp_query->request;
                //intval � Get the integer value of a variable
                /*http://php.net/manual/en/function.intval.php*/
                $posts_per_page = intval(get_query_var('posts_per_page'));
                //$posts_per_page = 1;
                //Retrieve variable in the WP_Query class.
                /*http://codex.wordpress.org/Function_Reference/get_query_var*/
                $paged = intval(get_query_var('paged'));
                $numposts = $wp_query->found_posts;
                $max_page = $wp_query->max_num_pages;


                //empty � Determine whether a variable is empty
                /*http://php.net/manual/en/function.empty.php*/
                if(empty($paged) || $paged == 0) {
                        $paged = 1;
                }

                $currently_showingfrom = ($paged-1)*$posts_per_page;
                $currently_showingto = $paged*$posts_per_page;
                if($currently_showingto >  $numposts) {
                        $currently_showingto = $numposts;
                }

                $pages_to_show = intval($custom_pagination['num_pages']);
                $larger_page_to_show = intval($custom_pagination['num_larger_page_numbers']);
                $larger_page_multiple = intval($custom_pagination['larger_page_numbers_multiple']);
                $pages_to_show_minus_1 = $pages_to_show - 1;
                $half_page_start = floor($pages_to_show_minus_1/2);
                //ceil � Round fractions up (http://us2.php.net/manual/en/function.ceil.php)
                $half_page_end = ceil($pages_to_show_minus_1/2);
                $start_page = $paged - $half_page_start;

                if($start_page <= 0) {
                        $start_page = 1;
                }

                $end_page = $paged + $half_page_end;
                if(($end_page - $start_page) != $pages_to_show_minus_1) {
                        $end_page = $start_page + $pages_to_show_minus_1;
                }
                if($end_page > $max_page) {
                        $start_page = $max_page - $pages_to_show_minus_1;
                        $end_page = $max_page;
                }
                if($start_page <= 0) {
                        $start_page = 1;
                }

                $larger_per_page = $larger_page_to_show*$larger_page_multiple;
                //round_num() custom function - Rounds To The Nearest Value.
                $larger_start_page_start = (round_num($start_page, 10) + $larger_page_multiple) - $larger_per_page;
                $larger_start_page_end = round_num($start_page, 10) + $larger_page_multiple;
                $larger_end_page_start = round_num($end_page, 10) + $larger_page_multiple;
                $larger_end_page_end = round_num($end_page, 10) + ($larger_per_page);

                if($larger_start_page_end - $larger_page_multiple == $start_page) {
                        $larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
                        $larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
                }
                if($larger_start_page_start <= 0) {
                        $larger_start_page_start = $larger_page_multiple;
                }
                if($larger_start_page_end > $max_page) {
                        $larger_start_page_end = $max_page;
                }
                if($larger_end_page_end > $max_page) {
                        $larger_end_page_end = $max_page;
                }
                if($max_page > 1 || intval($custom_pagination['always_show']) == 1) {
                        /*http://php.net/manual/en/function.str-replace.php */
                        /*number_format_i18n(): Converts integer number to format based on locale (wp-includes/functions.php*/
                        $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $custom_pagination['pages_text']);
                        $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
                        echo $before.'<div class="dir_pagination "><ul>'."\n";

                        if(!empty($pages_text)) {
                                echo '<li><span class="pages">'.$pages_text.'</span></li>';
                        }

                        $ppl = explode('"',get_previous_posts_link());
                        if (count($ppl) > 1) {
                                $ppl_url=$ppl[1];
                        }

                        //Displays a link to the previous post which exists in chronological order from the current post.
                        /*http://codex.wordpress.org/Function_Reference/previous_post_link*/



                        if( $paged > 1 )
                        {?>
                                <a href="<?php echo $ppl_url ?>" class="prev" style='font-size:12px'> <img src="<?php bloginfo('template_directory'); ?>/images/previouspostslink.png" style="width:14px; height:13px;margin-bottom:-2px;">  Prev  |</a>

                        <?php } ?>



                        <?php
                                //previous_posts_link($custom_pagination['prev_text']);


                        echo "<span> $paged of $max_page </span>";
                        if ($start_page >= 2 && $pages_to_show < $max_page) {
                                $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $custom_pagination['first_text']);
                                //esc_url(): Encodes < > & " ' (less than, greater than, ampersand, double quote, single quote).
                                /*http://codex.wordpress.org/Data_Validation*/
                                //get_pagenum_link():(wp-includes/link-template.php)-Retrieve get links for page numbers.
                                //echo '<li><a href="'.esc_url(get_pagenum_link()).'" class="first" title="'.$first_page_text.'">1</a></li>';
                                if(!empty($custom_pagination['dotleft_text'])) {
                                //  echo '<li class="expandli"><span class="expand">'.$custom_pagination['dotleft_text'].'</span></li>';
                                }
                        }

                        if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
                                for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
                                        $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $custom_pagination['page_text']);
                                        //echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" class="single_page" title="'.$page_text.'">'.$page_text.'</a></li>';
                                }
                        }

                        for($i = $start_page; $i  <= $end_page; $i++) {
                                if($i == $paged) {
                                        $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $custom_pagination['current_text']);
                                // echo '<li><span class="current">'.$current_page_text.'</span></li>';
                                } else {
                                        $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $custom_pagination['page_text']);
                                // echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" class="single_page" title="'.$page_text.'">'.$page_text.'</a></li>';
                                }
                        }

                        if ($end_page < $max_page) {
                                if(!empty($custom_pagination['dotright_text'])) {
                                        //echo '<li class="expandli"><span class="expand">'.$custom_pagination['dotright_text'].'</span></li>';
                                }
                                $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $custom_pagination['last_text']);
                                //echo '<li><a href="'.esc_url(get_pagenum_link($max_page)).'" class="last" title="'.$last_page_text.'">'.$max_page.'</a></li>';
                        }

$npl=explode('"',get_next_posts_link());
if (count($npl) > 1) {
$npl_url=$npl[1];
} else {
$npl_url=$npl[0];
}



                        if( $max_page > $paged ) {
                        //echo '<li class="next">';
                        ?>     

                        <a href="<?php echo $npl_url ?>" style='font-size:12px'>|  Next <img src="<?php bloginfo('template_directory'); ?>/images/nextpostslink.png" style="width:14px; height:13px;margin-bottom:-2px;"></a>
                        <?php }?>
                        <?php //next_posts_link($custom_pagination['next_text'], $max_page);
                        if( $max_page < $paged );
                                echo '</li>';
                        if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
                                for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
                                        $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $custom_pagination['page_text']);
                                        echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" class="single_page" title="'.$page_text.'">'.$page_text.'</a></li>';
                                }
                        }
                        echo '</ul></div>'.$after."\n";
                }
        }
}

I have tried with a for but did not managed to went ahead. Please help, I am a noob in wordpress. I researched about 10 stackoverflow posts, but did not managed to get it working.

This is what I have:

<?php
 global $wpdb;
 $category_ids = get_categories('taxonomy=cities&orderby=order&hide_empty=0');
 foreach($category_ids as $cat) { 
   $term_include = $wpdb->get_var($wpdb->prepare("SELECT term_include FROM $wpdb->terms WHERE term_id = %d ", $cat->term_id ));
   $link = get_term_link( $cat->slug, 'cities' );
   $image = z_taxonomy_image_url($cat->term_id, 'large');
   $category_heading = $cat->name;
   $excerpt = smart_excerpt(category_description($cat->term_id), 20);
   $category_post = the_category(' / ');
 }
 wp_custom_pagination();
?>
7
  • 2
    I don't understand you very well. Are you trying to paginate a list of terms?. If so, check this. It work on exactly the same principle Commented Aug 30, 2014 at 9:36
  • @PieterGoosen I am trying to paginate a list of categories from a custom taxonomy, but I am trying with wp_custom_pagination. Ok, will try that.
    – Adrian
    Commented Aug 30, 2014 at 9:37
  • wp_custom_pagination is not a native function. Where did you get this function from Commented Aug 30, 2014 at 9:41
  • Found it somewhere, but don't remember where
    – Adrian
    Commented Aug 30, 2014 at 9:45
  • 3
    The function you are trying to use for pagination uses the global variables from main wp query. It won't work for secondary queries like your list of categories. You have to investigate other method or function like the one linked by @PieterGoosen
    – cybmeta
    Commented Aug 30, 2014 at 9:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.