Ajax(JQuery)wp_Query分页返回-1

时间:2012-03-15 作者:Yash

我目前正在为wordpress开发一个ajaxified主题。我列出了特定类别中的帖子,但单击后分页返回-1。假设我将showposts设置为3,并且一个特定类别有2篇帖子,指向以前帖子的链接不会按预期显示。现在,如果该类别有5篇帖子,则会显示指向以前帖子的链接,这也是意料之中的,但当我单击它时,会返回-1。这是我的代码:

<?php
$category = get_category_by_slug($_POST[\'catname\']);
$wp_query = new WP_Query();
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$wp_query->query(\'cat=\'.$category->term_id.\'&showposts=1&paged=\'.$paged);
?>
<script>
$(\'.pagi a\').click(function(e)
   {
       e.preventDefault();
       var link = $(this).attr(\'href\');
       $(\'#b_contentwrapper\').load(link);
});
</script>
<?php
while ($wp_query->have_posts()) : $wp_query->the_post();
    get_the_title();
endwhile;
?>
<div class="pagi"><?php next_posts_link(\'&laquo; Older Entries\') ?></div>
<div class="pagi"><?php previous_posts_link(\'Newer Entries &raquo;\') ?></div>
<?php
?>
另外,上一篇文章的链接是http://domain.com/wp-admin/admin-ajax.php?paged=2 我怀疑这可能是问题的根源。

有人能帮忙吗?

谢谢

1 个回复
SO网友:Gecko

首先要做的是检查是否有影响搜索结果的插件。原因是我运行了“搜索一切”插件。虽然它旨在搜索更多结果并突出显示搜索词,但它将更改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一起使用,因为提供的链接是加载新页面的URL。我不得不提醒自己,使用AJAX时,页面不会重新加载以提供新内容。我创建了两个“链接”,它们再次调用ajax函数,但页码不同。现在,您可以在不使用WP的默认url参数的情况下分页搜索结果。需要记住的是,这将如何影响搜索引擎结果。

结束

相关推荐

是否通过`unctions.php`解除jQuery与WooCommerce的连接?

所以我用的是http://woothemes.com/woocommerce/ 电子商务插件,它在本地注册jQuery,与我从Google CDN默认的jQuery调用冲突。这花了很长时间,但我将问题隔离到以下几行代码中woocommerce.php: $suffix = defined(\'SCRIPT_DEBUG\') && SCRIPT_DEBUG ? \'\' : \'.min\'; $lightbox_en = (get_option(\'woocommer