Replacing global wp_query

时间:2012-07-03 作者:Ty Morton

我有一个网站,它使用自定义查询按国家搜索帖子。

搜索结果很好,但底部的分页反映了站点中的帖子总数,而不是搜索结果中的帖子总数。

管理此操作的插件使用全局$wp\\u查询来确定发布的数量,而不是自定义查询。默认情况下,其工作方式如下:

function wp_page_numbers($start = "", $end = "")
{
    global $wp_query;
    global $max_page;
    global $paged;
    if ( !$max_page ) { $max_page = $wp_query->max_num_pages; }
    if ( !$paged ) { $paged = 1; }

    $settings = get_option(\'wp_page_numbers_array\');
    $page_of_page = $settings["page_of_page"];
    $page_of_page_text = $settings["page_of_page_text"];
    $page_of_of = $settings["page_of_of"];

    $next_prev_text = $settings["next_prev_text"];
    $show_start_end_numbers = $settings["show_start_end_numbers"];
    $show_page_numbers = $settings["show_page_numbers"];

    $limit_pages = $settings["limit_pages"];
    $nextpage = $settings["nextpage"];
    $prevpage = $settings["prevpage"];
    $startspace = $settings["startspace"];
    $endspace = $settings["endspace"];

    if( $nextpage == "" ) { $nextpage = ">"; }
    if( $prevpage == "" ) { $prevpage = "<"; }
    if( $startspace == "" ) { $startspace = "..."; }
    if( $endspace == "" ) { $endspace = "..."; }

    if($limit_pages == "") { $limit_pages = "10"; }
    elseif ( $limit_pages == "0" ) { $limit_pages = $max_page; }
我尝试将其修改为:

if ($gallery_query){ $page_query=$gallery_query; }else {$page_query=$wp_query;}
    global $max_page;
    global $paged;
    if ( !$max_page ) { $max_page = $page_query->max_num_pages; }
    if ( !$paged ) { $paged = 1; }
但这不会返回任何页面。

理想情况下,我应该用$gallery\\u查询值替换$wp\\u查询值,但我不知道如何替换。显而易见的$wp_query = $gallery_query 不起作用。

有什么建议吗?

谢谢

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

首先,我会认真考虑选择另一个插件(它在over 2 years), 或利用WP的内置paginate_links.

但是,在寻求解决方案时,不妨尝试一下:

function wp_page_numbers_custom( $wp_query, $start = \'\', $end = \'\' )
{
    $_backup = array(
        \'wp_query\' => @ $GLOBALS[\'wp_query\'],
        \'max_page\' => @ $GLOBALS[\'max_page\'],
           \'paged\' => @ $GLOBALS[\'paged\'],  
    );

    $GLOBALS[\'wp_query\'] = $wp_query;
    $GLOBALS[\'max_page\'] = $wp_query->max_num_pages;
       $GLOBALS[\'paged\'] = max( $wp_query->get( \'paged\' ), 1 );

    wp_page_numbers( $start, $end );

    foreach ( $_backup as $var => $val )
        $GLOBALS[ $var ] = $val;
}

// Example usage: wp_page_numbers_custom( $gallery_query );

结束

相关推荐

Travel Blog Plugins

今年晚些时候,我将使用Wordpress创建一个关于我旅行的博客。我希望该博客具有以下功能我的帖子将被地理定位一张包含帖子位置的地图,可以单击地图上的各个点到达帖子</我正在寻找最好/最合适的插件。谢谢,艾尔。