最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

urls - Add addtional page parameter before loading the page

programmeradmin6浏览0评论

the problem I got: I need to force a list view for products which are shown on a specific page only for mobile devices, for me the perfect solution would be an additional url parameter -> '&product_view=list', before it is redirected to the page.

Thank you!

the problem I got: I need to force a list view for products which are shown on a specific page only for mobile devices, for me the perfect solution would be an additional url parameter -> '&product_view=list', before it is redirected to the page.

Thank you!

Share Improve this question asked Apr 20, 2019 at 9:42 SwitchITSwitchIT 1
Add a comment  | 

2 Answers 2

Reset to default 0

Set a variable

$product_view=isset($_GET['product_view']) ? $_GET['product_view'] : 'grid'; 

Now, how to keep it like this with pagination URL's.

echo paginate_links(array(
    'base' => add_query_arg('product_view', $product_view),
    'format' => '',
    'prev_text' => __('«'),
    'next_text' => __('»'),
    'total' => ceil($total / $items_per_page),
    'current' => $page,
    'add_args' => array(
        'category' => 'fruit',
        'color' => 'red'
    )
));

I hope this will help you to solve your problem.

Thank You,

Maybe you could try using the MobileDetect class (found in Github, I'm not affiliated) to detect if user is using a mobile device, and then add the url parameter with add_query_arg on template_redirect action. Something along these lines.

require_once 'Mobile_Detect.php'; // use correct path    
function prefix_add_list_view_param_on_mobile() {
  $detect = new Mobile_Detect;
  $is_product_view = true; // add logic
  // Any mobile device (phones or tablets).
  if ( $is_product_view && $detect->isMobile() && empty( $_GET['product_view'] ) ) {    
    wp_safe_redirect( esc_url( add_query_arg( 'product_view', 'list' ) ) ); // omitting url param results in the current URL being used (the value of $_SERVER['REQUEST_URI'])
    exit;
  }  
}
add_action( 'template_redirect', 'prefix_add_list_view_param_on_mobile' );

The code exapmle is untested and might require some tweaking.

发布评论

评论列表(0)

  1. 暂无评论