我不知所措,我发现了几个与此相关的悬而未决的问题。我有一个静态贴子页面,侧栏中有一个搜索表单。我想做的是键入一个关键字,然后“过滤”(使用ajax返回)只过滤适合搜索的帖子。我还希望它被分页。
我找到了几种操纵的方法$wp_query
这样我就可以对ajax调用使用相同的循环。不幸的是当我看global $wp_query
在函数中。在我的回调操作所在的php中,它基本上是null或初始化为默认参数。我在本地调试所有东西,我想知道这是否会影响任何东西。我还没有确认,但除非显式地发生了什么事情(比如导航到新页面),否则全局变量不应该保持不变吗?
这是函数中的回调。php
function cbSearch()
{
$counter=1;
$html="";
global $wp_query;
if ( have_posts() )
{
while ( have_posts() )
{
the_post();
$html.="<div class=\'news-page-item\'>";
$html.=the_title(\'<h2>\',\'</h2>\');
$date= strtoupper(get_the_date(\'F Y\'));
$subtitle = get_post_meta (get_the_ID(), \'subtitle\', $single = true);
$summary = get_the_excerpt();//get_post_meta (get_the_ID(), \'summary\', $single = true);
$html.="<div class=\'fg-grey f12 mt8\'>".$subtitle." - ".$date."</div>";
$upload_dir = wp_upload_dir();
$html.="<img class=\'mv25\' src=\'".$upload_dir[\'baseurl\']."/news-item.jpg\' alt=\'".get_the_title()."\' />";
$html.="<p class=\'f15\'>";
$html.=$summary;
$html.="<a href=\'".get_permalink()."\' class=\'fg-grey f11\'> › READ MORE</a>";
$html.="</p>";
if ($counter != count($posts))
{
$html.="<hr class=\'mv30 c888\' />";
}
++$counter;
$html.="</div>";
echo $html;
$html="";
}
}
$html.="<div id=\'news-page-nav\'>";
$html.="<span class=\'left\'>";
$html.=get_previous_posts_link(" P ");
$html.="</span>";
$html.="<span class=\'right\'>";
$html.=get_next_posts_link(" N ");
$html.="</span></div>";
echo $html;
die();
}
我在此函数中使用断点来查找
$wp_query
参数。
这是调用后立即执行var\\u转储时返回的内容global $wp_query;
object(WP_Query)[145]
public \'query\' => null
public \'query_vars\' =>
array
empty
public \'tax_query\' => null
public \'meta_query\' => boolean false
public \'queried_object\' => null
public \'queried_object_id\' => null
public \'request\' => null
public \'posts\' => null
public \'post_count\' => int 0
public \'current_post\' => int -1
public \'in_the_loop\' => boolean false
public \'post\' => null
public \'comments\' => null
public \'comment_count\' => int 0
public \'current_comment\' => int -1
public \'comment\' => null
public \'found_posts\' => int 0
public \'max_num_pages\' => int 0
public \'max_num_comment_pages\' => int 0
public \'is_single\' => boolean false
public \'is_preview\' => boolean false
public \'is_page\' => boolean false
public \'is_archive\' => boolean false
public \'is_date\' => boolean false
public \'is_year\' => boolean false
public \'is_month\' => boolean false
public \'is_day\' => boolean false
public \'is_time\' => boolean false
public \'is_author\' => boolean false
public \'is_category\' => boolean false
public \'is_tag\' => boolean false
public \'is_tax\' => boolean false
public \'is_search\' => boolean false
public \'is_feed\' => boolean false
public \'is_comment_feed\' => boolean false
public \'is_trackback\' => boolean false
public \'is_home\' => boolean false
public \'is_404\' => boolean false
public \'is_comments_popup\' => boolean false
public \'is_paged\' => boolean false
public \'is_admin\' => boolean false
public \'is_attachment\' => boolean false
public \'is_singular\' => boolean false
public \'is_robots\' => boolean false
public \'is_posts_page\' => boolean false
public \'is_post_type_archive\' => boolean false
public \'query_vars_hash\' => boolean false
public \'query_vars_changed\' => boolean true
public \'thumbnails_cached\' => boolean false
我知道ajax功能正在工作。有人能告诉我为什么$wp\\u查询会在页面加载和http请求之间的某个地方变成垃圾吗?
编辑:
在进一步考虑了一下逻辑之后,我认为典型的分页将不适用于ajax。由于next/previous\\u posts\\u link提供了链接的url,这意味着将根据页面模板加载$wp\\u查询参数url中的任何其他参数。只有当$wp\\u query知道它正在加载页面模板时,下一个/上一个\\u posts\\u链接才会返回http://domain.com/new/page/2/ 而不是http://domain.com/wp-admin/admin-ajax.php?paged=2.
我正在考虑使用get_posts()
在ajax调用中,返回一个页面的帖子,并创建下一个/上一个链接,这些链接调用ajax函数,偏移量表示页码。
SO网友:Gecko
根据Milo的输入,我成功地创建并测试了我的功能,符合问题的规范。一个AJAX函数,返回与搜索查询匹配的帖子并分页。首先要做的是检查是否有影响搜索结果的插件。原因是我运行了“搜索一切”插件。虽然它旨在搜索更多结果并突出显示搜索词,但它将更改new WP_Query();
使其无用。
这是用ajax加载帖子的回调函数。调用函数的工作方式与任何其他客户端请求类似,将结果回显到空div。
function cbSearch()
{
$counter = 1;
$max = 4;
$html = "";
//Variables passed through $_POST
$s = $_POST[\'src\'];
//the first time you call this function page number should be 1
//Additional pages/functions/links are created with this code
$page_num = $_POST[\'idx\'];
//Assuming the old $wp_query is basically null,
//there\'s no reason to copy previous parameters, just make a new one
$search_query = array(\'s\' => $s, \'posts_per_page\' => $max, \'paged\' => $page_num, \'post_type\' => \'post\', \'post_status\' => \'publish\');
$search = new WP_Query($search_query);
//use the object created, have_posts() alone will use global $wp_query
if ($search->have_posts())
{
//The variables required to figure out next or previous page
//Total number of post that match $s
$total = $search->found_posts;
//Post count for the page you\'re on
$post_count = $search->post_count;
//The maximum amount of pages to display all of the post results
$page_count = ceil($search->max_num_pages);
while ($search->have_posts())
{
//Setup post data !important
$search->the_post();
//Now your in the loop, use any loop functions you want
$title = highlight(get_the_title(), $s);
}
$html = "<div id=\'news-page-nav\'>";
//Since we know what page we\'re on we know where to go
$pre = $page_num - 1;
$nex = $page_num + 1;
if ($pre > 0)
{
$html .= "<span class=\'left fg-grey-light pointer\' onclick=\'searchPosts(" . $pre . ");return false;\' >P</span>";
}
if ($nex <= $page_count)
{
$html .= "<span class=\'right fg-grey-light pointer\' onclick=\'searchPosts(" . $nex . ");return false;\' >N</span>";
}
$html .= "</div>";
echo $html;
}
else
{
echo false;
}
die();
}
function highlight($str, $mts)
{
$keys = explode(" ", $mts);
$title = preg_replace(\'/(\' . implode(\'|\', $keys) . \')/iu\', \'<span class="bg-highlight">\\0</span>\', $str);
return $title;
}
我不得不提醒自己,使用AJAX时,页面不会重新加载以提供新内容。我创建了两个“链接”,它们再次调用ajax函数,但页码不同。现在,您可以在不使用WP的默认url参数的情况下对搜索结果分页。需要记住的是,这将如何影响搜索引擎结果。