具有分页功能的同一页面上的两个查询

时间:2018-07-20 作者:Bruno Andrade

我试图将两个查询放在具有分页的同一页上。但其中一个分页会干扰另一个分页。如何使用两个独立的分页来执行此操作

<div class="row list-group">
    <?php 

        function  query_group_by_filter($groupby){
            global $wpdb;

            return $wpdb->postmeta . \'.meta_value \';
        }
        global $paged, $wp_query, $wp;

        $args = wp_parse_args($wp->matched_query);
        if ( !empty ( $args[\'paged\'] ) && 0 == $paged ) {
            $wp_query->set(\'paged\', $args[\'paged\']);
            $paged = $args[\'paged\'];
        }                               
        $temp = $wp_query;
        $wp_query= null;
        add_filter(\'posts_groupby\', \'query_group_by_filter\');
        $args = array(
        \'post_type\' => \'a-casa\',
        \'posts_per_page\' => 1,
        \'paged\' => $paged,
        \'meta_key\'      => \'semana\',
        \'post_status\' => \'publish\',
        \'order\' => \'ASC\',
        );
        $wp_query = new WP_Query($args);                                    
        //$wp_query->query(\'post_type=post&posts_per_page=\'.$betubeVideoCount.\'&paged=\'.$paged);
        $current = -1;
        $current2 = 0;
        $post_id = $post->ID;
        remove_filter(\'posts_groupby\', \'query_group_by_filter\');
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++; ?>
    <div class="large-12 columns">
        <div style=\'text-align: center;\'> 
            <h3>Videos da Semana: <?php $atrizweek = get_field(\'semana\'); echo $atrizweek; ?></h3>

            <h4><?php 
                $atriz_id = get_field(\'atrizes\');

                $atriz = get_post( $atriz_id[0]);

            echo "<a style=\'text-align: center;\' href=\'#\'>{$atriz->post_title}</a>"; ?></h4>                    
        </div>
    </div>

    <div class="large-12 columns"><div class="pagination-semana">

        <div class="nav-previous alignleft"><?php next_posts_link( \'Semana Anterior\' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( \'Próxima Semana\' ); ?></div>

    </div></div>
    <div class="item large-4 medium-6 columns end <?php echo $myClass; ?>">

    </div><!--item large-4-->

    <?php endwhile; ?>


    <?php wp_reset_query(); ?>
</div><!--new-all-->

<!-- NEW MY CUSTOM-->
<div class="tabs-content" data-tabs-content="newVideos">
    <div class="tabs-panel is-active" id="new-all">
        <div class="row list-group">
            <?php 
                global $paged, $wp_query, $wp;
                $args = wp_parse_args($wp->matched_query);

                if ( !empty ( $args[\'paged\'] ) && 0 == $paged ) {
                    $wp_query->set(\'paged\', $args[\'paged\']);
                    $paged = $args[\'paged\'];
                }                               
                $temp = $wp_query;
                $wp_query= null;

                $args = array(
                \'post_type\' => \'a-casa\',
                \'posts_per_page\' => $betubeVideoCount,
                \'paged\' => $paged,
                \'meta_key\'      => \'semana\',
                \'meta_value\'    => $atrizweek,
                \'post_status\' => \'publish\',
                \'orderby\' => \'date\',
                );
                $wp_query = new WP_Query($args);    


                //$wp_query->query(\'post_type=post&posts_per_page=\'.$betubeVideoCount.\'&paged=\'.$paged);
                $current = -1;
                $current2 = 0;
                $post_id = $post->ID;

            ?>
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++; ?>
            <div class="item large-4 medium-6 columns end <?php echo $myClass; ?>">
                <div class="post thumb-border">
                    <div class="post-thumb">
                        <?php 
                            if( has_post_thumbnail()){
                                echo get_the_post_thumbnail();
                                }else{
                            ?>
                            <img src="<?php echo get_template_directory_uri() . \'/assets/images/nothumb.png\' ?>" alt="No Thumb"/>
                            <?php
                            }
                        ?>
                        <a href="<?php the_permalink(); ?>" class="hover-posts">
                            <span><i class="fa fa-play"></i><?php esc_html_e( \'Watch Video\', \'betube\' ); ?></span>
                        </a>
                    </div><!--post thumb-border-->
                </div><!--item large-4-->
                <?php endwhile; ?>

                <div class="large-12 columns"><?php 
                    global $wp_rewrite;         
                    $wp_query->query_vars[\'paged\'] > 1 ? $current = $wp_query->query_vars[\'paged\'] : $current = 1;

                    $classiera_pagination = array(
                    \'base\' => @add_query_arg(\'page\',\'%#%\'),
                    \'format\' => \'\',
                    \'total\' => $wp_query->max_num_pages,
                    \'current\' => $current,
                    \'show_all\' => false,
                    \'type\' => \'plain\',
                    );

                    if( $wp_rewrite->using_permalinks() )
                    $classiera_pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg(\'s\',get_pagenum_link(1) ) ) . \'page/%#%/\', \'paged\');

                    if( !empty($wp_query->query_vars[\'s\']) )
                    $classiera_pagination[\'add_args\'] = array(\'s\'=>get_query_var(\'s\'));

                    echo \'<div class="pagination">\' . paginate_links($classiera_pagination) . \'</div>\';         
                ?></div>
            </div><!--row list-group-->
            <?php wp_reset_query(); ?>
        </div><!--new-all-->        

1 个回复
SO网友:Trilok
Please use below code hope it\'s work for you 
<div class="row list-group">
    <?php 

        function  query_group_by_filter($groupby){
            global $wpdb;

            return $wpdb->postmeta . \'.meta_value \';
        }
        global $paged, $wp_query, $wp;

        $args = wp_parse_args($wp->matched_query);
        if ( !empty ( $args[\'paged\'] ) && 0 == $paged ) {
            $wp_query->set(\'paged\', $args[\'paged\']);
            $paged = $args[\'paged\'];
        }                               
        $temp = $wp_query;
        $wp_query= null;
        add_filter(\'posts_groupby\', \'query_group_by_filter\');
        $args = array(
        \'post_type\' => \'a-casa\',
        \'posts_per_page\' => 1,
        \'paged\' => $paged,
        \'meta_key\'      => \'semana\',
        \'post_status\' => \'publish\',
        \'order\' => \'ASC\',
        );
        $wp_query = new WP_Query($args);                                    
        //$wp_query->query(\'post_type=post&posts_per_page=\'.$betubeVideoCount.\'&paged=\'.$paged);
        $current = -1;
        $current2 = 0;
        $post_id = $post->ID;
        remove_filter(\'posts_groupby\', \'query_group_by_filter\');
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++; ?>
    <div class="large-12 columns">
        <div style=\'text-align: center;\'> 
            <h3>Videos da Semana: <?php $atrizweek = get_field(\'semana\'); echo $atrizweek; ?></h3>

            <h4><?php 
                $atriz_id = get_field(\'atrizes\');

                $atriz = get_post( $atriz_id[0]);

            echo "<a style=\'text-align: center;\' href=\'#\'>{$atriz->post_title}</a>"; ?></h4>                    
        </div>
    </div>

    <div class="large-12 columns"><div class="pagination-semana">

        <div class="nav-previous alignleft"><?php next_posts_link( \'Semana Anterior\' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( \'Próxima Semana\' ); ?></div>

    </div></div>
    <div class="item large-4 medium-6 columns end <?php echo $myClass; ?>">

    </div><!--item large-4-->

    <?php endwhile; ?>


    <?php wp_reset_postdata(); ?>
</div><!--new-all-->

<!-- NEW MY CUSTOM-->
<div class="tabs-content" data-tabs-content="newVideos">
    <div class="tabs-panel is-active" id="new-all">
        <div class="row list-group">
            <?php
                $args2 = wp_parse_args($wp->matched_query);

                if ( !empty ( $args2[\'paged\'] ) && 0 == $paged ) {
                    $wp_query->set(\'paged\', $args2[\'paged\']);
                    $paged = $args2[\'paged\'];
                }                               
                $temp = $wp_query;
                $wp_query= null;

                $args2 = array(
                \'post_type\' => \'a-casa\',
                \'posts_per_page\' => $betubeVideoCount,
                \'paged\' => $paged,
                \'meta_key\'      => \'semana\',
                \'meta_value\'    => $atrizweek,
                \'post_status\' => \'publish\',
                \'orderby\' => \'date\',
                );
                $wp_query2 = new WP_Query($args2);    


                //$wp_query->query(\'post_type=post&posts_per_page=\'.$betubeVideoCount.\'&paged=\'.$paged);
                $current = -1;
                $current2 = 0;
                $post_id = $post->ID;

            ?>
            <?php while ($wp_query2->have_posts()) : $wp_query2->the_post(); $current++; $current2++; ?>
            <div class="item large-4 medium-6 columns end <?php echo $myClass; ?>">
                <div class="post thumb-border">
                    <div class="post-thumb">
                        <?php 
                            if( has_post_thumbnail()){
                                echo get_the_post_thumbnail();
                                }else{
                            ?>
                            <img src="<?php echo get_template_directory_uri() . \'/assets/images/nothumb.png\' ?>" alt="No Thumb"/>
                            <?php
                            }
                        ?>
                        <a href="<?php the_permalink(); ?>" class="hover-posts">
                            <span><i class="fa fa-play"></i><?php esc_html_e( \'Watch Video\', \'betube\' ); ?></span>
                        </a>
                    </div><!--post thumb-border-->
                </div><!--item large-4-->
                <?php endwhile;  ?>

                <div class="large-12 columns"><?php 
                    global $wp_rewrite;         
                    $wp_query->query_vars[\'paged\'] > 1 ? $current = $wp_query->query_vars[\'paged\'] : $current = 1;

                    $classiera_pagination = array(
                    \'base\' => @add_query_arg(\'page\',\'%#%\'),
                    \'format\' => \'\',
                    \'total\' => $wp_query->max_num_pages,
                    \'current\' => $current,
                    \'show_all\' => false,
                    \'type\' => \'plain\',
                    );

                    if( $wp_rewrite->using_permalinks() )
                    $classiera_pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg(\'s\',get_pagenum_link(1) ) ) . \'page/%#%/\', \'paged\');

                    if( !empty($wp_query->query_vars[\'s\']) )
                    $classiera_pagination[\'add_args\'] = array(\'s\'=>get_query_var(\'s\'));

                    echo \'<div class="pagination">\' . paginate_links($classiera_pagination) . \'</div>\';         
                ?></div>
            </div><!--row list-group-->
            <?php   wp_reset_postdata(); ?>
        </div><!--new-all-->    
结束

相关推荐

如何在class-wp-query.php中跟踪核心函数is_page()&is_Single上的通知警告

在我的生产站点上,我的这些产品已经超支了:PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3728 PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3730 PHP No