替换wp_query值

时间:2012-07-03 15:56:39

标签: wordpress wordpress-plugin

我的网站使用自定义查询按国家/地区搜索帖子。

搜索结果回复正常,但底部的分页反映了网站中的帖子总数,而不是搜索结果中的帖子总数。

管理它的插件使用全局$ wp_query来确定帖子数,而不是自定义查询。默认情况下,它的工作方式如下:

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_query值替换$ wp_query值,但我不知道如何。显而易见的$wp_query = $gallery_query不起作用。

有什么建议吗?

谢谢!

TY

2 个答案:

答案 0 :(得分:0)

感谢TheDeadMedic

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 );

它完全解决了我的问题!

答案 1 :(得分:0)

我遇到了与WP Page Numbers插件相同的问题。该问题的另一个解决方案是修改搜索结果的$ wp_query,然后在需要返回原始文件时执行wp_reset_query。