<?php
/*
Plugin Name: Rotate Latest Post
Description: Rotates the latest post titles on each page refresh or visit, excluding the current post if on a post page.
Version: 1.2
Author: Gee
*/

function rotate_latest_post() {
    global $post;

    // Query for the latest posts, excluding the current post if on a single post page
    $args = array(
        'numberposts' => 6, // Fetch one extra post to ensure 5 unique ones can be displayed
        'orderby' => 'date',
        'order' => 'DESC',
        'post__not_in' => (is_single() ? array($post->ID) : array())
    );

    $latest_posts = wp_get_recent_posts($args);

    // Shuffle and select 5 posts to display
    if (!empty($latest_posts)) {
        shuffle($latest_posts);
        $latest_posts = array_slice($latest_posts, 0, 5);

        echo '<div class="rotated-post-titles">';
        foreach ($latest_posts as $random_post) {
            $post_id = $random_post['ID'];
            $post_title = get_the_title($post_id);
            $post_link = get_permalink($post_id);

            echo '<h6><a href="' . esc_url($post_link) . '">' . esc_html($post_title) . '</a></h6>';
        }
        echo '</div>';
    }
}

add_shortcode('rotate_latest_post', 'rotate_latest_post');