我有一个为Wordpress中的分页循环结果构建的函数。除了包含在 search.php 模板文件中之外,此功能在所有情况下都在整个站点中运行。按钮显示但单击时不起作用。
我无法弄清楚为什么在这种情况下它不起作用。我的代码如下:
a) functions.php 主题模板文件中的功能:
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)."'>«</a>";
// if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</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 ' ';
}
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)."'>›</a>";
// if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</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 ' ';
}
echo "</div>";
echo "</div>\n";
} }
b)在 category-loop.php 主题模板文件(工作)中调用函数:
<?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)在 search.php (不工作)中调用函数:
<?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(); ?>
我试图找出为什么这个分页不适用于搜索的原因而且我很困难。任何帮助将非常感激。非常感谢。
示例:https://famtravsticky1.staging.wpengine.com/?s=beach
示例(工作):https://famtravsticky1.staging.wpengine.com/holiday-destinations/indian-ocean/mauritius/