WordPress文章页侧面添加最近浏览的文章功能

nZone 2023年8月16日更新于12 月前 共672字 75行代码 预计4分钟 评论 45
阿里云折扣】服务器活动对比【腾讯云秒杀

通过cookies保存博客访客自己的浏览历史,有两种实现方式,纯代码和插件方式。纯代码功能较为简单,插件则功能强大一点。

纯代码实现

将以下代码添加到主题目录下的“functions.php”文件:

/* 最近浏览*/
$lvp_cookie_expire = 360; // cookie过期时间,默认值是360天
$lvp_number_of_posts = 10; // 显示篇数,默认值是10。
$lvp_recognize_pages = true;

function lvp_header() {
    if (is_single()) {
        lvp_setcookie();
    } else if (is_page()) {
        global $lvp_recognize_pages;
        if ($lvp_recognize_pages === true) {
            lvp_setcookie();
        }
    }
}

function lvp_setcookie() {
    global $wp_query;
    $lvp_post_ID = $wp_query->post->ID;

    $lvp_cookiearray = isset($_COOKIE["WP-LastViewedPosts-coding3min"])
        ? unserialize(stripslashes($_COOKIE["WP-LastViewedPosts-coding3min"]))
        : array();

    if (in_array($lvp_post_ID, $lvp_cookiearray)) {
        $lvp_key = array_search($lvp_post_ID, $lvp_cookiearray);
        array_splice($lvp_cookiearray, $lvp_key, 1);
    }
    array_unshift($lvp_cookiearray, $lvp_post_ID);

    global $lvp_number_of_posts;
    while (count($lvp_cookiearray) > $lvp_number_of_posts) {
        array_pop($lvp_cookiearray);
    }

    $lvp_blog_url = preg_replace('#^https?://(www\.)?#', '', home_url());
    $lvp_path_url = parse_url(home_url(), PHP_URL_PATH);

    global $lvp_cookie_expire;
    setcookie("WP-LastViewedPosts-coding3min", serialize($lvp_cookiearray), (time() + ($lvp_cookie_expire * 86400)), $lvp_path_url, $lvp_blog_url, 0);
}

function lvp_recently_viewed() {
    echo '<ul class="viewed_posts">';
    if (isset($_COOKIE["WP-LastViewedPosts-coding3min"])) {
        $lvp_post_IDs = unserialize(stripslashes($_COOKIE["WP-LastViewedPosts-coding3min"]));
        global $wpdb;
        foreach ($lvp_post_IDs as $value) {
            $lvp_get_title = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM $wpdb->posts WHERE ID = %d LIMIT 1", $value));
            if ($lvp_get_title) {
                echo "<li><a href=\"" . get_permalink($value) . "\" title=\"" . esc_attr($lvp_get_title) . "\">" . esc_html($lvp_get_title) . "</a></li>\n";
            }
        }
    }
    echo '</ul>';
}

add_action('get_header', 'lvp_header');

再进入“外观”的“小工具”里,在“正文侧边栏”中添加增强文本,内容代码如下:

<?php
if (function_exists('lvp_recently_viewed')) {
    if (isset($_COOKIE["WP-LastViewedPosts-coding3min"])) {
        lvp_recently_viewed();
    }
}
?>

也可以把标题添加到代码中:

<?php
if (function_exists('lvp_recently_viewed')) {
    if (isset($_COOKIE["WP-LastViewedPosts-coding3min"])) {
        echo '<div class="recently-viewed-widget">';
        echo '<h4 class="widget-title">最近浏览</h4>';
        lvp_recently_viewed();
        echo '</div>';
    }
}
?>

这样即可实现近期浏览的文章功能了。

插件实现

安装 Last Viewed PostsWP Recent Views 插件即可实现,这两个插件都很多年没更新了,分别两年和10年,但还能用。

Last Viewed Posts 提供基本的最近浏览过的文章列表功能,显示用户最近浏览过的帖子。较为简单,专注于核心功能。

WP Recent Views 提供更多功能,除了有文章历史外,还能显示页面和媒体等,包括更多的定制性选项,允许你自定义外观、样式、排序方式以及其他设置。它可能提供更丰富的功能来满足不同需求。

如果你希望简单添加一个基本的最近浏览功能,Last Viewed Posts 是一个不错的选择。如果你需要更多的定制性和功能,可以考虑使用 WP Recent Views 或类似功能更强大的插件。

weinxin
公众号
闹着玩下网
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: