纯代码实现WordPress博客显示访客最近浏览的文章

# 版权声明

西柚乐ciyol.cn发布的部分内容包括:教程、文章、图片、视频、软件、程序、源码等搜集于互连网,仅供学习交流,严禁用于商业用途,请于24小时内删除,如喜欢请购买作者正版,享受售后服务!

# 详细介绍

释放双眼,带上耳机,听听看~!
今天在久伴博客看到一个效果,就是侧边栏可以显示博客访客最近浏览过的文章,特分享给大家。

纯代码实现WordPress博客显示访客最近浏览的文章

代码部署

将以下代码添加到functions.php文件

<?php
/* 可选参数: */
$zg_cookie_expire = 360; // cookie过期时间,默认值是360天
$zg_number_of_posts = 10; // 显示篇数,默认值是10。
$zg_recognize_pages = 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 '<ul class="viewed_posts">';
    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 "<li><a href=\"". get_permalink($value+0) . "\" title=\"". $zg_title_out->post_title . "\">". $zg_title_out->post_title . "</a></li>\n";
            }
        }
    } else {
    }
    echo '</ul>';
}
add_action('get_header','zg_lwp_header');
?>

以上代码添加完毕,请复制以下代码到自己想显示的地方粘贴


<?php if (function_exists('zg_recently_viewed')): if (isset($_COOKIE["WP-LastViewedPosts"])) { ?>
<h2>Last viewed posts</h2>
<?php zg_recently_viewed(); ?>
<?php } endif; ?>

 

纯代码实现WordPress博客显示访客最近浏览的文章-西柚乐
最后修改: 2019-08-15 10:27

人已赞赏
WordPress教程

纯代码为WordPress添加音乐播放器 —— WordPress教程

2019-6-15 11:52:47

WordPress教程

升级WordPress出现"The site is experiencing technical difficulties."错误

2019-6-19 14:22:14

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索