• I have a function built for paginated loop results in WordPress. This function is working throughout the site in all cases with the exception of its inclusion in the search.php template file. The buttons are displaying but not working when clicked.

    I can not figure out why it would not be working in this case. My code is as follows:

    
    function kriesi_pagination($pages = '', $range = 2, $query = false) {  
    $showitems = ($range * 2)+1;  
    
    global $paged;
    if(empty($paged)) $paged = 1;
    
    if($query == false) {       
        global $wp_query;
        $query = $wp_query;
    }
    
    if($pages == '') {
        $pages = $query->max_num_pages;
        if(!$pages) {
            $pages = 1;
        }
    }   
    
    if(1 != $pages) {
        echo "<div class='loop-pagination'>";
    
            // if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
            // if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
    
            echo "<div class='loop-pagination--button'>";
                if($paged > 1) { 
                        echo "<a href='javascript:void(0)' data-page='" . ($paged - 1) . "' class='loop-pagination--arrow-left'>NEWER POSTS</a>";
                } else {
                    echo '&nbsp;';
                }
            echo "</div>";
    
            echo "<div class='loop-pagination--numbers'>";
            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='javascript:void(0)' data-page='" . ($i) . "' class='inactive' >".$i."</a>";
                }
            }
            echo "</div>";
    
            // if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
            // if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
    
            echo "<div class='loop-pagination--button'>";
                if($paged < $pages) { 
                    echo "<a href='javascript:void(0)' data-page='" . ($paged + 1) . "' class='loop-pagination--arrow-right'>OLDER POSTS</a>";
            // changed on 26th June 2017 to fix errors
            // 'javascript:void(0)' and '$paged + 1' on line 1688, from # and $paged - 1
            // also fixed error on line 1688 - # to 'javascript:void(0)'
                } else {
                    echo '&nbsp;';
                }
            echo "</div>";
    
        echo "</div>\n";
    } }
    
    

    search.php (where it does not work):

    
    <?php
    /*
    Template Name: Search Template
    */
    ?>
    
    <?php get_header(); the_post(); ?>
    
    <?php
    
    $post_image = get_field('default_background_image', 'option');
    
    if(get_search_query()) {
        $page_title = 'Search Results for "'.get_search_query().'"';
    } else {
        $page_title = 'Search';
    }
    
    load_component('hero-block', array(
        'title' => $page_title,
        'background_image' => $post_image,
        'text' => false,
        'tagline' => false,
        'hero_tint' => false
    ));
    
    ?>
    
    <div class="container">
    
        <div class="page-container">
    
            <form method="get" class="destination-search" action="<?php echo home_url( '/' ); ?>">
                <input type="text" name="s" value="<?php echo get_search_query() ?>" class="destination-search--input" id="search-input" placeholder="Search">
                <input type="submit" value="submit" class="destination-search--submit">
            </form>
    
            <?php
            $letter_array = array('a'=>array(),'b'=>array(),'c'=>array(),'d'=>array(),'e'=>array(),'f'=>array(),'g'=>array(),'h'=>array(),'i'=>array(),'j'=>array(),'k'=>array(),'l'=>array(),'m'=>array(),'n'=>array(),'o'=>array(),'p'=>array(),'q'=>array(),'r'=>array(),'s'=>array(),'t'=>array(),'u'=>array(),'v'=>array(),'w'=>array(),'x'=>array(),'y'=>array(),'z'=>array());
    
            $idObj = get_category_by_slug('holiday-destinations');
    
            if($idObj) {
                $id = $idObj->term_id;
    
                $categories = get_term_children( $id, 'category');
    
                foreach($categories as $category) {
                    $cats = get_category($category);
                    if($cats->category_parent !== $id) {
                        $first_letter = strtolower(substr($cats->name ,0, 1));
                        $letter_array[$first_letter][] = $cats;
                    }
                }
            }
    
            function order_cities($a, $b) {
                return strtolower($a->name) > strtolower($b->name);
            }
    
            ?>
    
            <?php
            $search_term = strtolower(get_search_query());
            foreach($letter_array as $letter => $cities) {
                if(count($cities) > 0) {
                    usort($cities, 'order_cities');
                    foreach($cities as $city) {
                        if ($search_term && strpos(strtolower($city->name), $search_term) !== false) {
                            if($destination_found == false) {
                                echo '<h2 class="card-grid--title">Destinations matching your search:</h2>';
                                echo '<div class="card-grid align-left">';
                                $destination_found = true;
                            }
    
                            $post_image = get_template_directory_uri().'/assets/images/post-placeholder.png';
                            if(get_field('cat_header_image', 'term_' . $city->term_id)) {
                                $post_image = get_field('cat_header_image', 'term_' . $city->term_id)['sizes']['post-thumb'];
                            }
    
                            load_component('post-card', array(
                                'link' => get_category_link($city->term_id),
                                'image' => $post_image,
                                'title' => $city->name,
                                'date' => false,
                                'category' => get_cat_name($city->parent),
                                'excerpt' => false,
                                'icon' => false,
                                'link_target' => false,
                                'sharing' => false,
                                'count' => false
                            ));
                        }
                    }
                }
            }
    
            if($destination_found == true) {
                echo '</div>';
            }
            ?>
    
            <?php $totalPosts = $wp_query->post_count; $postCount = 0;
            // var_dump($wp_query);
            if(have_posts()) : ?>
                <h2 class="card-grid--title">Articles matching your search:</h2>
                <div class="card-grid">
                    <?php rewind_posts(); while(have_posts()) : the_post(); $postCount++; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
                        <?php
                        $post_image = get_template_directory_uri().'/assets/images/post-placeholder.png';
                        if(has_post_thumbnail()) {
                            $post_image = get_the_post_thumbnail_url(get_the_ID(), 'post-thumb');
                        }
    
                        $author_data = get_userdata(get_the_author_meta('ID'));
                        load_component('post-card', array(
                            'link' => get_the_permalink(),
                            'image' => $post_image,
                            'title' => comp_title_filter(get_the_title()),
                            'date' => get_the_modified_date('jS M Y'),
                            'category' => false,
                            'excerpt' => wpautop(get_the_excerpt()),
                            'icon' => false,
    'link_target' => false,
                            'sharing' => true,
                            'count' => false
                        ));
                        ?>
    
                    <?php endwhile; ?>
                </div>
                <?php if($postCount == $totalPosts) : ?>
                    <div class="static-nav-container">
                        <?php echo kriesi_pagination('', 2, $the_query); ?>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
    
        </div>
    
    </div>
    
    <?php get_footer(); ?>
    

    I have tried to find the reason why this pagination is not working on search and I am very stuck. Any help would be much appreciated. Many thanks.

    Example: https://famtravsticky1.staging.wpengine.com/?s=beach

    Example (working): https://famtravsticky1.staging.wpengine.com/holiday-destinations/indian-ocean/mauritius/

    • This topic was modified 7 years, 9 months ago by chrisbrosnan.
    • This topic was modified 7 years, 9 months ago by chrisbrosnan. Reason: code not displaying correctly
    • This topic was modified 7 years, 9 months ago by chrisbrosnan.
    • This topic was modified 7 years, 9 months ago by chrisbrosnan.
Viewing 1 replies (of 1 total)
  • Hello @cdbrosnan,

    I noticed the results on search page https://famtravsticky1.staging.wpengine.com/?s=beach is not wrapped in div with id “ajax-load”. If you wrap the div with class “card-grid” with <div id="ajax-load"></div> then it should work.

    Just compare the html using browser inspect element of results update area of both the links you will notice search page don’t have any div with “ajax-load” and so it is generating javascript error. You can see errors to in browser console.

    I hope this helps.

    Thanks.

Viewing 1 replies (of 1 total)
  • The topic ‘Pagination not working on WordPress search template’ is closed to new replies.