Есть ли в WordPress функционал, который бы показывал статьи которые смотрел посетитель за последнее время
По типу Моя лента, или просмотренное?
Просто PHP-блок для вставки в шаблон
В functions.php (один раз)
// Трекинг просмотров add_action('wp_footer', 'track_viewed_posts_js'); function track_viewed_posts_js() { if (!is_single()) return; ?> <script> (function() { const postId = <?php echo get_the_ID(); ?>; const key = 'viewed_posts'; let viewed = JSON.parse(localStorage.getItem(key) || '[]'); viewed = viewed.filter(id => id !== postId); viewed.unshift(postId); if (viewed.length > 30) viewed = viewed.slice(0, 30); localStorage.setItem(key, JSON.stringify(viewed)); })(); </script> <?php } // AJAX-обработчик add_action('wp_ajax_get_viewed_posts', 'get_viewed_posts_handler'); add_action('wp_ajax_nopriv_get_viewed_posts', 'get_viewed_posts_handler'); function get_viewed_posts_handler() { $ids = isset($_POST['post_ids']) ? $_POST['post_ids'] : ''; if (empty($ids)) wp_send_json_success(array()); $post_ids = array_filter(array_map('intval', explode(',', $ids))); if (empty($post_ids)) wp_send_json_success(array()); $query = new WP_Query(array( 'post__in' => $post_ids, 'orderby' => 'post__in', 'posts_per_page' => count($post_ids), 'post_status' => 'publish', )); $posts = array(); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $posts[] = array( 'title' => get_the_title(), 'url' => get_permalink(), 'date' => get_the_date('d.m.Y'), ); } wp_reset_postdata(); } wp_send_json_success($posts); }В шаблон страницы (куда хочешь)
<div id="my-feed-list">Загрузка...</div> <script> (function() { const viewed = JSON.parse(localStorage.getItem('viewed_posts') || '[]'); const container = document.getElementById('my-feed-list'); if (!viewed.length) { container.innerHTML = '<p>Вы пока ничего не смотрели.</p>'; return; } fetch('<?php echo admin_url('admin-ajax.php'); ?>', { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: 'action=get_viewed_posts&post_ids=' + viewed.join(',') }) .then(r => r.json()) .then(data => { if (!data.success || !data.data.length) { container.innerHTML = '<p>Ничего не найдено.</p>'; return; } let html = '<ul style="list-style:none;padding:0;">'; data.data.forEach(p => { html += `<li style="padding:10px 0;border-bottom:1px solid #eee;"> <a href="${p.url}" style="text-decoration:none;color:#333;display:block;"> <strong>${p.title}</strong> <span style="display:block;font-size:13px;color:#888;margin-top:3px;">${p.date}</span> </a> </li>`; }); html += '</ul>'; container.innerHTML = html; }) .catch(() => container.innerHTML = '<p>Ошибка загрузки.</p>'); })(); </script>