dreamfly0514 2015-09-06 07:03
浏览 30

如何在PHP代码中更改活动类

index.php

<?php
     $cur_page = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1; //get curent page
     $offers_per_page = couponxl_get_option( 'offers_per_page' );

     $args = array(
        'post_type'     => 'offer',
        'posts_per_page'=> $offers_per_page,
        'post_status'   => 'publish',
        'meta_key'      => 'offer_expire',
        'orderby'       => 'meta_value_num',
        'page'         => $cur_page,
        'order'         => 'ASC',
        'meta_query'    => array(
        'relation' => 'AND',
         array(
            'key' => 'offer_store',
            'value' => get_the_ID(),
            'compare' => '='
         ),
         array(
            'key' => 'offer_start',
            'value' => current_time( 'timestamp' ),
            'compare' => '<='
         ),
         array(
            'relation' => 'OR',
                array(
                     'key' => 'offer_expire',
                     'value' => current_time( 'timestamp' ),
                     'compare' => '>='
                ),
                array(
                     'key' => 'offer_expire',
                     'value' => '-1',
                     'compare' => '='
                )
          ),
          array(
              'relation' => 'OR',
              array(
                  'key' => 'deal_status',
                  'value' => 'sold_out',
                  'compare' => '!='
              ),
              array(
                 'key' => 'deal_status',
                 'value' => 'sold_out',
                 'compare' => 'NOT EXISTS'
              ),
           ),
        )
       );


       if( !empty( $offer_type ) ){
           $args['meta_query'][] = array(
           'key' => 'offer_type',
           'value' => $offer_type,
           'compare' => '='
       );
       }

       $offers = new WP_Query( $args );
       $page_links_total =  $offers->max_num_pages;
       $pagination_args = array(
       'end_size' => 2,
       'mid_size' => 2,
       'total' => $page_links_total,
       'current' => $cur_page, 
       'prev_next' => false,
       'type' => 'array'
       );
       if( isset( $offer_type ) ){
           $pagination_args['format'] = !get_option( 'permalink_structure' ) ? '?page=%#%' : 'page/%#%';
       }
       $page_links = paginate_links( $pagination_args );
           $pagination = couponxl_format_pagination( $page_links );
           if( $offers->have_posts() ){                    
                $col = 6;
                if( $offer_view == 'list' ){
                    $col = 12;
                }
           ?>
<?php if( !empty( $pagination ) ): ?>
    <div class="col-sm-<?php echo esc_attr( $col ) ?> masonry-item">
        <ul class="pagination">
             <?php echo $pagination; ?>
        </ul>
    </div>
<?php endif; ?>

function.php

function couponxl_format_pagination( $page_links ){    
    global $couponxl_slugs;
    $list = '';
    if (!empty($page_links)) {
        foreach ($page_links as $page_link) {
            if (strpos($page_link, 'page-numbers current') !== false) {
                $page_link = str_replace("<span class='page-numbers current'>", '<a href="javascript:;">', $page_link);
                $page_link = str_replace('</span>', '</a>', $page_link);
                $list .= '<li class="active">'.$page_link.'</li>';
            } else {
                if (get_query_var($couponxl_slugs['coupon']) && get_option('permalink_structure')) {
                    $page_link = preg_replace('#coupon\\/(.*?)/#i', '', $page_link, -1);
                } else {
                    $page_link = preg_replace('#(&\\#038\\;' . $couponxl_slugs['coupon'] . '|&' . $couponxl_slugs['coupon'] . ')=(.*?)&#i', '&', $page_link);
                    $page_link = preg_replace('#(&\\#038\\;' . $couponxl_slugs['coupon'] . '|&' . $couponxl_slugs['coupon'] . ')=(.*?)\'#i', '\'', $page_link );
                }               
                $list .= '<li>' . $page_link . '</li>';
            }
        }
    }
    return $list;
}

When I change page number so page changed but page active class not change is always active page number 1 and when I click again page number 1 is not working is activate and not change in deactivate. Please tell me what I do pagination not working properly and active code not working properly.

follow this link:- http://www.cuponswall.in/store/flipkart-coupons-offers

pagination not working on this page

  • 写回答

1条回答 默认 最新

  • duanouyong4228 2015-09-06 08:33
    关注

    I think the code should be changed like blow:

    (file: function.php)

    function couponxl_format_pagination( $page_links, $cur_page ){    
        global $couponxl_slugs;
        $list = '';
        if (!empty($page_links)) {
            foreach ($page_links as $page_link) {
                if (strpos($page_link, 'page-numbers current') !== false) {
                    $page_link = str_replace("<span class='page-numbers current'>", '<a href="javascript:;">', $page_link);
                    $page_link = str_replace('</span>', '</a>', $page_link);
                    $act_class = $page_link_n = '';  
                    $page_link_n = strip_tags($page_link);                
                    if($cur_page == $page_link_n) $act_class=' class="active"';
                    $list .= '<li'.$act_class.'>'.$page_link.'</li>';
                } else {
                    if (get_query_var($couponxl_slugs['coupon']) && get_option('permalink_structure')) {
                        $page_link = preg_replace('#coupon\\/(.*?)/#i', '', $page_link, -1);
                    } else {
                        $page_link = preg_replace('#(&\\#038\\;' . $couponxl_slugs['coupon'] . '|&' . $couponxl_slugs['coupon'] . ')=(.*?)&#i', '&', $page_link);
                        $page_link = preg_replace('#(&\\#038\\;' . $couponxl_slugs['coupon'] . '|&' . $couponxl_slugs['coupon'] . ')=(.*?)\'#i', '\'', $page_link );
                    }               
                    $list .= '<li>' . $page_link . '</li>';
                }
            }
        }
        return $list;
    }
    

    update: use the function strip_tags() can get the page number of $page_link,so make the judge work.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分