用代碼實現WordPress流覽歷史功能

這個方法不需要使用JS
而且也比較簡單。
這是使用
Cookies記錄瀏覽者曾經流覽過的文章,展示遊覽歷史的功能
第一、開啟主題的functions.php貼下入以下代碼
  [cc lang="php"] //文章遊覽歷史,第一、相關參數: $zg_cookie_expire = 360; // //cookie過期的時間,預設值是360天 $zg_number_of_posts = 10; // 顯示篇數,預設值是10。 $zg_recognize_pages = true; //是否也要設置cookies?預設值是true function zg_lwp_header() { if (is_single()) { zg_lw_setcookie(); } else if (is_page()) { global $zg_recognize_pages; if ($zg_recognize_pages === true) { zg_lw_setcookie(); } } } function zg_lw_setcookie() { global $wp_query; $zg_post_ID = $wp_query->post->ID; if (! isset($_COOKIE["WP-LastViewedPosts"])) { $zg_cookiearray = array($zg_post_ID); } else { $zg_cookiearray = unserialize(preg_replace('!s:(d+):"(.*?)";!e', "'s:'.strlen('$2').':"$2";'", stripslashes($_COOKIE["WP-LastViewedPosts"]))); if (! is_array($zg_cookiearray)) { $zg_cookiearray = array($zg_post_ID); } } if (in_array($zg_post_ID, $zg_cookiearray)) { $zg_key = array_search($zg_post_ID, $zg_cookiearray); array_splice($zg_cookiearray, $zg_key, 1); } array_unshift($zg_cookiearray, $zg_post_ID); global $zg_number_of_posts; while (count($zg_cookiearray) > $zg_number_of_posts) { array_pop($zg_cookiearray); } $zg_blog_url_array = parse_url(get_bloginfo('url')); $zg_blog_url = $zg_blog_url_array['host']; $zg_blog_url = str_replace('www.', '', $zg_blog_url); $zg_blog_url_dot = '.'; $zg_blog_url_dot .= $zg_blog_url; $zg_path_url = $zg_blog_url_array['path']; $zg_path_url_slash = '/'; $zg_path_url .= $zg_path_url_slash; global $zg_cookie_expire; setcookie("WP-LastViewedPosts", serialize($zg_cookiearray), (time()+($zg_cookie_expire*86400)), $zg_path_url, $zg_blog_url_dot, 0); } function zg_recently_viewed() { echo '
  • '; if (isset($_COOKIE["WP-LastViewedPosts"])) { $zg_post_IDs = unserialize(preg_replace('!s:(d+):"(.*?)";!e', "'s:'.strlen('$2').':"$2";'", stripslashes($_COOKIE["WP-LastViewedPosts"]))); foreach ($zg_post_IDs as $value) { global $wpdb; $zg_get_title = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE ID = '$value+0' LIMIT 1"); foreach($zg_get_title as $zg_title_out) { echo "
  • post_title . "">". wp_trim_words($zg_title_out->post_title,15) . "
  • n"; } } } else { } echo '
'; } add_action('get_header','zg_lwp_header'); [/cc]
第二、再來增加css的語法樣式
[cc lang="php"] .toky_oooo li a:hover{color:#fff;background:#0099cb;border-color:#0099cb;} .toky_oooo li{float:left;list-style-type:none;-moz-border-radius:0;-webkit-border-radius:0;border-radius:0;} .toky_oooo li a{display:block;width:100%;height:100%;color:#232323;text-decoration:none;background:#f1f1f1 url(http://playpsss.myweb.hinet.net/img/subbga100.jpg);-moz-box-shadow:1px 1px 3px #666;-webkit-box-shadow:1px 1px 3px #666;box-shadow:1px 1px 3px #666;-moz-border-radius:8px;-webkit-border-radius:8px;border-radius:8px;margin:6px 5px 9px 0;padding:5px;} [/cc]  
第三、調用的代碼
[cc lang="php"]

Last viewed posts

[/cc]

評論 (0)

此處尚未發表評論

留言

  1. 以遊客身份發表評論。 註冊登入到您的帳戶。
附件 (0 / 3)
分享您的位置