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!

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,

0

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.

  • got it!!! appreciate your help! – SwitchIT Apr 20 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.