向wp_Query生成的表添加分页

时间:2015-02-28 作者:kat_indo

我花了几个小时试图找到解决这个问题的方法,但我无法找到答案。我有一个显示用户数据的表,我正在尝试向其中添加分页。

我曾尝试在数组中添加“paged”,但当我单击链接时,会得到“Page not found”。

如果我理解正确,那么问题是因为我生成了一个表而不是“调用”帖子。

以下是我现在拥有的:

    <?php if ( !function_exists(\'gb_is_user_merchant_role\') || !gb_is_user_merchant_role( array( \'deal_admin\' ) ) ): ?>
<div class="dashboard_container section main_block">
<?php   
// Purchase history
    if ( gb_account_merchant_id() ) {
        $deals = null;
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $args = array(
            \'post_type\' => gb_get_deal_post_type(),
            \'post__in\' => gb_get_merchants_deal_ids(gb_account_merchant_id()),
            \'post_status\' => \'publish\',
            \'gb_bypass_filter\' => TRUE,
            \'posts_per_page\' => 5, // return this many
            \'paged\' => $paged,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => gb_get_deal_cat_slug(),
                    \'field\'    => \'id\',
                    \'terms\'    => array( 81,82,83,84,85,86,87,88,89,90,91,92 ),
                    \'operator\' => \'NOT IN\',
                ),
            ),

        );
        if ( isset( $_GET[\'filter\'] ) && $_GET[\'filter\'] != \'-1\' ) {
            $args[\'tax_query\'][] = array(
                \'taxonomy\' => gb_get_deal_cat_slug(),
                \'field\' => \'id\',
                \'terms\' => array( $_GET[\'filter\'] ),                    
                );
        }
        $deals = new WP_Query($args);
        if ($deals->have_posts()) {
            ?>

        <?php if ( !function_exists(\'gb_is_user_merchant_role\') || !gb_is_user_merchant_role( array( \'coupon_admin\' ) ) ): ?>
            <span class="specialLink_here" style="font-size:16px;"><a href="<?php gb_merchant_purchases_report_url( gb_account_merchant_id() ) ?>" class="report_button"><?php gb_e(\'Purchase History\') ?></a>  |  </span>
            <?php if ( function_exists( \'gb_sales_summary_report_url\' ) ): ?>
                <span class="specialLink_here" style="font-size:16px;"><a href="<?php gb_sales_summary_report_url() ?>" class="report_button"><?php gb_e(\'Sales Summary Report\') ?></a>  |  </span>                 
            <?php endif ?>
        <?php endif ?>

        <?php if ( !function_exists(\'gb_is_user_merchant_role\') || gb_is_user_merchant_role( array( \'merchant_admin\', \'sales_admin\' ) ) ): ?>
            <?php if ( function_exists( \'sec_get_users_report_url\' ) ): ?>
                <span class="specialLink_here" style="font-size:16px;"><a href="<?php echo sec_get_users_report_url() ?>" class="report_button"><?php gb_e(\'Customer Report\') ?></a></span>                 
            <?php endif ?>
        <?php endif ?>

        <table class="report_table merchant_dashboard" style="margin-top:20px;"><!-- Begin .purchase-table -->

        <thead>
        <tr>
        <th class="contrast th_status" style="padding:10px;"><?php gb_e(\'Status\'); ?>     </th>
        <th class="purchase-purchase_deal_title-title contrast" style="padding:10px;"><?php gb_e(\'Deal\'); ?></th>
        <th class="contrast th_total_sold" style="padding:10px;"><?php gb_e(\'Total Sold\'); ?></th>
        <th class="contrast th_published" style="padding:10px;"><?php gb_e(\'Published\'); ?></th>
        <th class="contrast th_category" style="padding:10px;">
            <form action="" method="get" >
                <?php
                    $selected = ( isset( $_GET[\'filter\'] ) && $_GET[\'filter\'] != \'\' ) ? $_GET[\'filter\'] : 0 ;
                    $args = array(
                    \'show_option_none\'   => gb__(\'Category Filter\'),
                    \'orderby\'            => \'name\',
                    \'hide_empty\'         => 1,
                    \'exclude\'            => \'81,82,83,84,85,86,87,88,89,90,91,92\', // comma separated list of ids.
                    \'echo\'               => 0,
                    \'name\'               => \'filter\',
                    \'selected\'           => $selected,
                    \'taxonomy\'           => gb_get_deal_cat_slug() );
                    $select = wp_dropdown_categories( $args );
                    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange=\'return this.form.submit()\'>", $select);
                    echo $select;
                    ?>
                <noscript><div><input type="submit" value="View" /></div></noscript>
            </form>
        </th>
        <th class="contrast th_reports" style="padding:10px;"><?php gb_e(\'Reports\'); ?></th>
        </tr>
        </thead>

        <tbody>

        <?php
        while ($deals->have_posts()) : $deals->the_post();

            // Build an array of the deal\'s categories.             
            $category_array = array();
            $cats = gb_get_deal_categories( get_the_ID() );
            foreach ( $cats as $cat ) {
                $category_array[] = \'<a href="\'.get_term_link( $cat->slug, gb_get_deal_cat_slug() ).\'">\'.$cat->name.\'</a>\';
            } ?>
            <tr id="published_deal_<?php the_ID() ?>">

            <td class="td_status">

                <?php if ( !function_exists(\'gb_is_user_merchant_role\') || !gb_is_user_merchant_role( array( \'sales_admin\' ) ) || ( gb_is_user_merchant_role( array( \'sales_admin\' ) ) && in_array( gb_get_status(), array( \'open\', \'closed\' ) ) ) ): ?>
                    <span class="alt_button<?php if (gb_get_status() == \'closed\') echo \' contrast_button\' ?>"><a href="<?php the_permalink() ?>"><?php echo gb_get_status() ?></a></span>
                    <br/>

                    <?php if ( !function_exists(\'gb_is_user_merchant_role\') || !gb_is_user_merchant_role( array( \'coupon_admin\', \'sales_admin\' ) ) ): ?>
                        <a href="#" class="deal_suspend_button alt_button contrast_button" rel="<?php the_ID() ?>"><?php gb_e(\'Suspend\') ?></a>
                    <?php endif ?>  
                <?php endif ?>  
            </td>

            <td class="purchase_deal_title">
            <?php the_title() ?>
            <br/>
            <a href="<?php the_permalink() ?>" target="_blank"><?php gb_e(\'View Deal\') ?></a>
            <?php if ( !function_exists(\'gb_is_user_merchant_role\') || !gb_is_user_merchant_role( array( \'coupon_admin\', \'sales_admin\' ) ) ): ?>
                <a href="<?php gb_deal_edit_url() ?>" target="_blank"><?php gb_e(\'Edit\') ?></a>
            <?php endif ?>
            </td> 

            <td class="td_total_sold"><?php gb_number_of_purchases() ?></td>

            <td class="td_published"><p><?php printf( gb__(\'Published: %s\'), get_the_date() ) ?></p><p><?php printf( gb__(\'Modified: %s\'), get_the_modified_date() ) ?></p></td>

            <td class="td_category"><?php echo implode( \', \', $category_array ) ?></td>

            <td class="td_reports">
            <?php if ( !function_exists(\'gb_is_user_merchant_role\') || !gb_is_user_merchant_role( array( \'coupon_admin\' ) ) ): ?>
                <span class="report_button"><?php gb_merchant_purchase_report_link() ?></span>
            <?php endif ?>
            <span class="report_button"><?php gb_merchant_voucher_report_link() ?></span>
            </td>

            </tr>
            <?php
        endwhile; ?>
        </tbody>
        </table><!-- End .purchase-table -->

        <?php
        } else {
            echo \'<p>\'.gb__(\'No sales info.\').\'</p>\';
        }
    } else {
        echo \'<p>\'.gb__(\'Restricted to Businesses.\').\'</p>\';
    }
?>

<?php if (  $deals->max_num_pages > 1 ) : ?>
                    <div id="nav-below" class="navigation clearfix">
                        <div class="nav-previous"><?php next_posts_link( gb__( \'<span class="meta-nav">&larr;</span> Older deals\' ), $deals->max_num_pages ); ?></div>
                        <div class="nav-next"><?php previous_posts_link( gb__( \'Newer deals <span class="meta-nav">&rarr;</span>\' ), $deals->max_num_pages ); ?></div>
                    </div><!-- #nav-below -->
                <?php endif; ?>
                <?php wp_reset_query(); ?>  

2 个回复
SO网友:Pieter Goosen

正如评论中已经指出的那样

  • gb_bypass_filter 不是的有效参数WP_Query. 如果要抑制过滤器对查询的影响,请添加\'suppress_filters\' => true 到您的查询参数

  • previous_posts_link() 不接受两个参数,仅接受一个。不像next_posts_link(), 它没有第二个$max_pages 参数这样就可以从函数中删除该部分

    使用时WP_Query, 您应该使用wp_reset_postdata(), 不wp_reset_query(). 后者用于query_posts 你永远不应该使用它。

    如果这是一个静态frontpage,您应该使用page 对您的paged 参数,非paged

    我再次查看了您的代码,您的代码似乎有点脱节。进行以下调整

    将分页移动到线的正下方endwhile</table> , 取决于要显示分页的位置。然而,后者看起来是正确的位置

    移动wp_reset_postdata() 在您的分页下方,这一切都应该介于endwhile 以及} else {. 原因是,当没有帖子时,您要重置什么:-)

    除此之外,您的代码应该正常工作并分页。如果没有,请尝试以下操作

    添加suppress_filters 您的查询的参数。这将是一个测试,以查看您是否没有修改查询的外部筛选器

    打开调试,检查是否有任何明显的错误和错误

    访问永久链接设置页面,再次刷新永久链接

    转储自定义查询(var_dump($deals);) 检查所有输入和输出是否符合预期。注意max_num_pages 并确保您有多个页面

    逐个停用所有插件,以消除可能导致问题的原因。此外,请清除所有缓存。还可以在捆绑主题上尝试您的代码

    除此之外,很难说是什么导致了您的问题

SO网友:Jason Murray

这个previous_posts_linknext_posts_link 函数用于归档页面分页,它们都检查is_single 不是true,因此不适用于任何类型的单个帖子、页面或自定义帖子类型。

您需要使用paginate_links 而是:http://codex.wordpress.org/Function_Reference/paginate_links

结束

相关推荐

wp_options table value

我试图理解wordpress的行为。当我在管理面板中添加标签时,mysql会执行一系列查询。基本上:更新wp_terms更新wp_term_taxonomy 和udate公司wp_options 使用此类查询INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`) VALUES ( \'project_location_children\', \'a:7:{i