我有一个可以正确返回结果的自定义搜索查询,但当我尝试对结果分页时,它会显示第一页的内容。当我单击“下一步”或页面编号时,搜索页面将重新加载,链接将从localhost/wordpress/search/更改?search\\u input=艺术(&U);搜索=
到
localhost/wordpress/search/page/2/?search\\u input=艺术(&U);搜索虽然第1页仍不可搜索, 页面内容保持不变。我尝试了文档Codex,YouTube教程,StackExchange, 还有很多其他的。
我曾尝试手动修改“paged”参数,将其设置为2,然后设置为3,它正确显示了第二页和第三页,而第1页仍然无法读取。顺便说一句,我把搜索查询放在一个函数中,并为该站点使用了一个免费的HTML模板(我正在接受培训)。谁能看出我的错在哪里吗。这是代码`
<div class="col-sm-9 padding-right">
<div class="features_items">
<?php
$book ;
$search_text = get_query_var(\'search_input\');
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$book = bbx_search($search_text,$paged);
$post_count = $book ->post_count;
echo \'<h2 class="title text-center">Search Results : \'.$post_count .\'</h2>\';
?>
<?php
while($book -> have_posts()){
$book->the_post();
$book_price = get_post_meta( get_the_ID(), \'book_price\',true);
$author_name = get_post_meta( get_the_ID(), \'author_name\',true);
echo \'<div class="col-sm-4">\';
echo \'<div class="product-image-wrapper">\';
echo \'<div class="single-products">\';
echo \'<div class="productinfo text-center">\';
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),\'thumbnail\' ); // or the_post_thumbnail(\'medium\');
if ($image) :
echo \'<img src="\'. $image[0].\'" alt="" />\';
endif;
echo \'<h2> $\'.$book_price .\'</h2>\';
echo \'<p>\'. the_title().\'</p>\';
echo \'<a href="\'.esc_url( add_query_arg( \'book_id\', get_the_ID(), site_url(\'/cart\'))).\'" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
</div>\';
} ?>
<nav class="prev-next-posts">
<?php
echo paginate_links(array (
\'total\'=> $book->max_num_pages
)); ?>
</nav>
<?php new WP_Query(); ?>
</div>
</div>
</div>
</div>
</section>
//-------------在函数文件中
function bbx_search($search_text, $paged){
$args1 = array(
\'post_type\' => \'book\',
\'post_status\'=> \'publish\',
\'order\'=> \'DSC\',
\'orderby\' => \'publish_date\',
\'posts_per_page\' => 3,
\'paged\' => $paged,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'author_name\', \'value\' => $search_text,
\'type\' => \'text\',\'compare\' => \'like\'),
array(
\'key\' => \'book_description\',\'value\' => $search_text,
\'type\' => \'text\',\'compare\' => \'like\'),
array(
\'key\' => \'bbx_category\',\'value\' => $search_text,
\'type\' => \'text\',\'compare\' => \'like\'),
array(
\'key\' => \'post_tag\',\'value\' => $search_text,
\'type\' => \'text\',\'compare\' => \'like\'),
)
);
return new WP_Query( $args1 );
}