用代碼實現WordPress流覽歷史功能
點擊數: 1923
文章目錄
- 這個方法不需要使用JS
- 而且也比較簡單。
- 這是使用
- Cookies記錄瀏覽者曾經流覽過的文章,展示遊覽歷史的功能
- 第一、開啟主題的functions.php貼下入以下代碼
- '; 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 "
- 第二、再來增加css的語法樣式
- 第三、調用的代碼
這個方法不需要使用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 '