admin管理员组

文章数量:1431317

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.

本文标签: urlsAdd addtional page parameter before loading the page