我不知所措,我发现了几个与此相关的悬而未决的问题。我有一个静态贴子页面,侧栏中有一个搜索表单。我想做的是键入一个关键字,然后“过滤”(使用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函数,偏移量表示页码。