2016-09-14 70 views
2

我展示這個我更新用戶在薈萃行動wp_footer之前登錄的用戶與下面的代碼,當然最近瀏覽過的產品:秀最近查看的產品非登錄用戶WordPress的

$rv = get_user_meta(get_current_user_id(), 'recently_viewed', true); 

但是,即使用戶未登錄,我也需要顯示最近查看過的產品。有什麼方法可以通過get_option()/ update_option()或其他方式來實現嗎?

回答

1

我決定使用cookie顯示最近查看的未登錄用戶的產品列表。我在下面的代碼中我functions.php文件,並在單品頁,我從餅乾越來越ID值,並使用get_post()函數來顯示信息:

function rv_products_non_logged_in(){ 
    $rv_posts = array(); 
    if (is_singular('product-items') && !is_user_logged_in()){ 
     if(isset($_COOKIE['rv_products']) && $_COOKIE['rv_products']!=''){ 
      $rv_posts = unserialize($_COOKIE['rv_products']); 
      if (! is_array($rv_posts)) { 
       $rv_posts = array(get_the_ID()); 
      }else{ 
       $rv_posts = array_diff($rv_posts, array(get_the_ID())); 
       array_unshift($rv_posts,get_the_ID()); 
      } 
     }else{ 
      $rv_posts = array(get_the_ID()); 
     } 
     setcookie('rv_products', serialize($rv_posts) ,time() + (DAY_IN_SECONDS * 31),'/'); 
    } 
} 
add_action('template_redirect', 'rv_products_non_logged_in'); 

我希望這會幫助別人!

相關問題