the paged param is allways 0

时间:2014-06-14 作者:Toni Michel Caubet

我列出了一个名为ad 使用get_posts wich工作正常,

我的问题是我不能阅读paged param,我需要它才能启用分页,

问题是:

get_query_var(\'paged\')$paged 都是0

url如下所示:

http://webiste.com/ads/?paged=2

我甚至想把query_vars

function query_vars($public_query_vars) {
    $public_query_vars[] = "paged";
}
add_filter(\'query_vars\', \'query_vars\');
你知道我错过了什么吗?

-编辑-

这就是我正在做的:

在里面functions.php

function get_anunciantes($params,$debug = false) {
    if ($debug ) {
        echo \'<pre style="background:#333"> \';
        print_r( $params );
        echo \'</pre>\';
    }
    $area_slug = $params[\'area_slug\'];
    $category_slug = $params[\'category_slug\'];
    if ( !empty ( $category_slug ) ) {
        $category_params = array(
            \'taxonomy\' => \'category\',
            \'field\' => \'slug\',
            \'terms\' => array("$category_slug")
        );  
    } else {
        $category_params = array();
    }
    if ( !empty ( $area_slug ) ) {
        $area_params = array(
            \'taxonomy\' => \'area\',
            \'field\' => \'slug\',
            \'terms\' => array("$area_slug")
        );  
    } else {
        $area_params = array();
    }

    if ( !empty( $area_params ) && !empty( $category_params ) ){
        $tax_params = array(
            \'relation\' => \'AND\',
            $area_params, 
            $category_params
        );
    } else if ( !empty( $area_params ) ){
        $tax_params = array($area_params);
    } else {
        $tax_params = array();
    }

    $today = time(); /* 1407375843 */
    $current_page = $params[\'current_page\'];
    /*$ipp = 5;
    $offset = $current_page * $ipp;*/
    $myposts = get_posts( array (
            \'posts_per_page\'   => 5,
            \'paged\'     =>  $current_page,
            /*\'showposts\' => -1,*/
            \'post_type\' => \'ad\',
            \'post_status\' => \'publish\',
            \'tax_query\' => $tax_params,
            \'meta_query\' => array(
                    array(
                        \'key\' => \'wpcf-start-date\',
                        \'value\' => $today,
                        \'compare\' => \'<=\',
                        \'type\' => \'NUMERIC\'
                    ),
                    array(
                        \'key\' => \'wpcf-end-date\',
                        \'value\' => $today,
                        \'compare\' => \'>\',
                        \'type\' => \'NUMERIC\'
                    )
                )
         )
    );

    $data = array(
        \'myposts\' => $myposts,
        \'pages\' => 6 /* static, i haven\'t done it yet */
    );
    return $data;


}
在模板中ads.php 使用页面的:

<div class="entry-content">
    <?php the_content(); ?>
    <?php echo $area_slug.\' > \'.$category_slug; ?>
    <ul>
        <?php 
        $params = array(
            \'area_slug\' => $area_slug,
            \'category_slug\' => $category_slug,
            \'current_page\' => get_query_var(\'current_page\')
        );
        $result = get_anunciantes($params,false);
        $myposts = $result[\'myposts\'];
        foreach ($myposts as $mypost) { ?>
            <li>
                <h3><a href="<?php echo get_permalink($mypost->ID ); ?>"><?php _e( $mypost->post_title ); ?></a></h3>
                <?php echo get_the_post_thumbnail($mypost->ID, \'thumbnail\'); ?>
                <p class="phone"><?php echo $meta[\'wpcf-phone\'][0]; ?></p>
            </li>
        <?php } ?> 
    </ul>   
    Paged <?php echo get_query_var(\'paged\'); echo \'-\'.$paged /* ALLWASY 0 */ ?>
    <?php
    for ($i = 1; $i <= $result[\'pages\']; $i++) {
        echo \'<a href="?paged=\'.$i.\'" class="\'.( $current_page == $i ? \'current\' : \'\' ).\'">\'.$i.\'</a>\';
    } ?>            
</div>

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

get\\u query\\u var()函数查看的是页面的主查询,您在这里实际上没有使用该查询。您正在使用自定义查询。因此,请尝试查看$result中的内容。

结束

相关推荐

Pagination URL order

我正在尝试按分类法为自定义帖子类型创建过滤器。当我使用过滤器选项时,它会创建一个不起作用的URL:/projecten-test/?branche=industrie/page/2/.但当我手动输入此url时,它确实起作用:/projecten-test/page/2/?branche=industrie我该如何自动完成这一点呢?我应该更改将php GET添加到URL的方法吗?我应该更改此函数底部的wp\\U重写吗//在自定义post typefunction paginate()上使用分页{全局$wp\