dst8922 2017-07-04 10:51
浏览 36

分页不适用于Wordpress搜索模板

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:

a) Function in functions.php theme template file:

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>
";
} }

b) Call to function in category-loop.php theme template file (working):

<?php

// i.e. Explore More of ..... block

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$the_query = new WP_Query( 
    array( 
        'post_type' => 'post', 
        'posts_per_page' => 3,
        'cat' => $data['cat']
    )
);

if ( $the_query->have_posts() ) {
    if($data['title']) {
        echo '<h2 class="card-grid--title">' . $data['title'] . '</h2>';
    }

global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
$arr = explode("/page/", $current_url);
$push_url_base = $arr[0];

echo '<div id="ajax-load" data-post-type="post" data-category="' . $data['cat'] . '">';
    echo '<div class="card-grid align-left owl-carousel card-container">';

        while ( $the_query->have_posts() ) {
            $the_query->the_post();

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

            $cat = get_the_category();

            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' => get_cat_name($cat[count($cat) - 1]->category_parent),
                // 'category' => 'Example',
                'excerpt' => false,
                'icon' => find_post_icon(get_the_ID()),
                'link_target' => false,
                'sharing' => true,
                'count' => false
            ));
        }

    echo '</div>';

    wp_reset_postdata();
    kriesi_pagination('', 2, $the_query);

echo '</div>'; } ?>

c) Call to function in search.php (not working):

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 求数学坐标画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站