飞流 发表于 2025-10-10 17:09:02

子比主题 – 侧边文章热榜功能

这是一款子比主题侧边文章热榜的功能,可以展开并且还可以返回上一页,这款功能还是不错的,唯一有问题的就是图标显示不全,有技术的自行部署吧。




代码部署
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢到里面即可!
function activity_ranking($time_range) {
    $args = array(
      'posts_per_page' => 10,
      'meta_key' => 'views',
      'orderby' => 'meta_value_num',
      'order' => 'DESC',
      'date_query' => array(
            array(
                'after' => $time_range
            )
      )
    );
    $query = new WP_Query($args);

    if ($query->have_posts()) {
      echo '<div class="rank-box">';
      $counter = 1;
      while ($query->have_posts()) {
            $query->the_post();
            echo '<a class="rank-item js-rank" href="' . get_permalink() . '" rel="external nofollow"rel="external nofollow" target="_blank">';
            echo '<span class="rank-number">' . $counter . '</span>';
            echo zib_post_thumbnail('','rank-thumbnail');
            echo '<div class="rank-info">';
            echo '<div class="rank-title">' . get_the_title() . '</div>';
            echo '<div class="rank-meta">';
            echo '<span class="rank-views">' . get_post_view_count('', '') . svg_icon('eye', 12) . '</span>';
            echo '<span class="rank-date">' . human_time_diff(get_the_time('U'), current_time('timestamp')) . '前</span>';
            echo '</div>';
            echo '</div>';
            echo '</a>';
            $counter++;
      }
      echo '</div>';
    } else {
      echo '<div class="rank-empty">';
      echo svg_icon('chart', 40);
      echo '<p>暂无热门内容</p>';
      echo '</div>';
    }
    wp_reset_postdata();
}

/**
* SVG图标函数
*/
function svg_icon($name, $size = 16) {
    $icons = [
      'eye' => '<svg width="'.esc_attr($size).'" height="'.esc_attr($size).'" viewBox="0 0 24 24" fill="currentColor"><path d="M12 9a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0 8a5 5 0 0 1-5-5 5 5 0 0 1 5-5 5 5 0 0 1 5 5 5 5 0 0 1-5 5m0-12.5C7 4.5 2.7 7.6 1 12c1.7 4.4 6 7.5 11 7.5s9.3-3.1 11-7.5c-1.7-4.4-6-7.5-11-7.5z"/></svg>',
      'chart' => '<svg width="'.esc_attr($size).'" height="'.esc_attr($size).'" viewBox="0 0 24 24" fill="currentColor"><path d="M3 13h8V3H3v10zm0 8h8v-6H3v6zm10 0h8V11h-8v10zm0-18v6h8V3h-8z"/></svg>',
      'trend' => '<svg width="'.esc_attr($size).'" height="'.esc_attr($size).'" viewBox="0 0 24 24" fill="currentColor"><path d="M16 6l2.3 2.3-4.8 4.8-4-4L2 16.6 3.4 18l6-6 4 4 6.3-6.3L22 12V6z"/></svg>',
      'arrow-left' => '<svg width="'.esc_attr($size).'" height="'.esc_attr($size).'" viewBox="0 0 24 24" fill="currentColor"><path d="M20 11H7.8l5.6-5.6L12 4l-8 8 8 8 1.4-1.4L7.8 13H20v-2z"/></svg>'
    ];
   
    return isset($icons[$name]) ? $icons[$name] : '';
}


add_action('wp_head', function() {
      echo '<style>
      .rank-views svg,
      .rank-button-icon svg {
            margin-left: 3px;
            vertical-align: middle;
      }
      
      .rank-empty svg {
            opacity: 0.5;
            margin-bottom: 10px;
      }
      
      .rank-float-container {
            position: fixed;
            top: '.(!empty($options['ranking_top']) ? $options['ranking_top'].'px' : '150px').';
            '.(!empty($options['ranking_position']) && $options['ranking_position'] == 'right' ? 'right: 20px;' : 'left: 20px;').'
            z-index: 999;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
      }
      
      /* 排行榜按钮 */
      .rank-button {
            width: 60px;
            height: 60px;
            background-color: #fff;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            margin-bottom: 15px;
            border-radius: 50%;
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: #333;
            overflow: hidden;
      }
      
      .rank-button:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
      }
      
      .rank-button.active {
            width: 300px;
            height: auto;
            border-radius: 16px;
            padding-bottom: 0;
      }
      
      .rank-button-icon {
            font-size: 20px;
            margin: 10px 0 5px;
            transition: all 0.3s ease;
      }
      
      .rank-button.active .rank-button-icon {
            display: none;
      }
      
      .rank-button-text {
            font-size: 12px;
            margin-bottom: 10px;
            transition: all 0.3s ease;
      }
      
      .rank-button.active .rank-button-text {
            display: none;
      }
      
      .rank-button-title {
            display: none;
            padding: 15px;
            font-size: 16px;
            font-weight: 600;
            color: #fff;
            background: linear-gradient(135deg, #6e8efb, #a777e3);
            border-top-left-radius: 16px;
            border-top-right-radius: 16px;
            width: 100%;
            box-sizing: border-box;
      }
      
      .rank-button.active .rank-button-title {
            display: flex;
            align-items: center;
            justify-content: center;
      }
      
      /* 排行榜内容 */
      .rank-box {
            display: none;
            max-height: 400px;
            overflow-y: auto;
            background-color: #fff;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            border-bottom-left-radius: 16px;
            border-bottom-right-radius: 16px;
            padding: 15px;
            scrollbar-width: thin;
            scrollbar-color: #ddd transparent;
      }
      
      .rank-box::-webkit-scrollbar {
            width: 6px;
      }
      
      .rank-box::-webkit-scrollbar-track {
            background: transparent;
      }
      
      .rank-box::-webkit-scrollbar-thumb {
            background-color: #ddd;
            border-radius: 3px;
      }
      
      .rank-button.active .rank-box {
            display: block;
            animation: fadeIn 0.3s ease;
      }
      
      /* 单个排名项 */
      .rank-item {
            display: flex;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #f0f0f0;
            text-decoration: none;
            color: #333;
            transition: all 0.2s ease;
      }
      
      .rank-item:last-child {
            border-bottom: none;
      }
      
      .rank-item:hover {
            background-color: #f9f9f9;
            transform: translateX(3px);
      }
      
      .rank-number {
            width: 24px;
            height: 24px;
            background-color: #f0f0f0;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            font-weight: bold;
            margin-right: 12px;
            flex-shrink: 0;
      }
      
      .rank-item:nth-child(1) .rank-number {
            background-color: #ffd700;
            color: #fff;
      }
      
      .rank-item:nth-child(2) .rank-number {
            background-color: #c0c0c0;
            color: #fff;
      }
      
      .rank-item:nth-child(3) .rank-number {
            background-color: #cd7f32;
            color: #fff;
      }
      
      .rank-thumbnail {
            width: 60px;
            height: 60px;
            border-radius: 8px;
            object-fit: cover;
            margin-right: 12px;
            flex-shrink: 0;
      }
      
      .rank-info {
            flex-grow: 1;
            overflow: hidden;
      }
      
      .rank-title {
            font-size: 14px;
            font-weight: 500;
            line-height: 1.4;
            margin-bottom: 4px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
      }
      
      .rank-meta {
            display: flex;
            font-size: 12px;
            color: #999;
      }
      
      .rank-views {
            margin-right: 10px;
            display: flex;
            align-items: center;
      }
      
      .rank-date {
            display: flex;
            align-items: center;
      }
      
      .rank-empty {
            padding: 30px 0;
            text-align: center;
            color: #999;
      }
      
      .rank-empty p {
            margin: 0;
            font-size: 14px;
      }
      
      /* 返回按钮 */
      .back-button {
            position: relative;
            overflow: hidden;
      }
      
      .back-button::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            opacity: 0;
            transition: opacity 0.3s ease;
      }
      
      .back-button:hover::after {
            opacity: 0.2;
      }
      
      /* 响应式设计 */
      @media (max-width: 768px) {
            .rank-float-container {
                bottom: 20px;
                top: auto;
                left: 20px;
                right: auto;
            }
            
            .rank-button {
                width: 50px;
                height: 50px;
            }
            
            .rank-button.active {
                width: calc(100vw - 40px);
                border-radius: 12px;
            }
            
            .rank-box {
                max-height: 60vh;
            }
      }
      
      /* 动画 */
      @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
      }
      </style>';
    }
);

add_action('wp_footer', function() {
      echo '<script>
      document.addEventListener("DOMContentLoaded", function() {
            // 排行榜切换
            var rankButton = document.getElementById("rank-toggle-button");
            if(rankButton) {
                rankButton.addEventListener("click", function(e) {
                  e.stopPropagation();
                  rankButton.classList.toggle("active");
                });
               
                // 点击页面其他地方关闭排行榜
                document.addEventListener("click", function(e) {
                  if(rankButton.classList.contains("active") && !rankButton.contains(e.target)) {
                        rankButton.classList.remove("active");
                  }
                });
            }
            
            // 返回按钮
            var backBtn = document.getElementById("rank-back-button");
            if(backBtn) {
                backBtn.addEventListener("click", function() {
                  window.location.href = "'.(!empty($options['ranking_back_link']) ? esc_url($options['ranking_back_link']) : 'https://www.xkzhi.com').'";
                });
               
                // 如果不是首页则显示返回按钮
                var currentPageUrl = window.location.href;
                var homepageUrl = "'.(!empty($options['ranking_back_link']) ? esc_url($options['ranking_back_link']) : 'https://www.xkzhi.com').'";
                if (currentPageUrl !== homepageUrl && !currentPageUrl.startsWith(homepageUrl + "/")) {
                  backBtn.style.display = "flex";
                } else {
                  backBtn.style.display = "none";
                }
            }
      });
      </script>';
      
      // 输出HTML结构
      echo '<div class="rank-float-container">';
      
      // 返回按钮
      echo '<div id="rank-back-button" class="rank-button back-button" style="display: none;">';
      echo '<span class="rank-button-icon">'.svg_icon('arrow-left', 20).'</span>';
      echo '<span class="rank-button-text">返回</span>';
      echo '<div class="rank-button-title">返回首页</div>';
      echo '</div>';
      
      // 排行榜按钮
      echo '<div id="rank-toggle-button" class="rank-button">';
      echo '<span class="rank-button-icon">'.svg_icon('trend', 20).'</span>';
      echo '<span class="rank-button-text">热榜</span>';
      echo '<div class="rank-button-title">热门榜单</div>';
      echo '<div class="rank-box">';
      activity_ranking('1 month ago');
      echo '</div>';
      echo '</div>';
      
      echo '</div>';
    }
);


页: [1]
查看完整版本: 子比主题 – 侧边文章热榜功能