子比主题 – 杂志轮博文章显示

## 简介

* 纯代码实现:无需安装任何插件,不增加数据库负担
* 自动更新:每次发布新文章,轮播自动显示最新内容
* SEO 友好:标准 HTML 结构,图片带 alt 属性,链接可点击
* 适配 Zibll:自动继承主题配色(–theme-color),支持深色模式
* 性能优化:CDN 加载 Owl Carousel,图片懒加载,不影响页面速度

## 图片展示

![](https://cs.chario.cn/wp-content/uploads/202602/45691770368616.webp)

## 安装教程

1. 通过 FTP 或 宝塔面板/主机文件管理器 进入网站目录
2. 找到 /wp-content/ 文件夹
3. 创建新文件夹:mu-plugins(如果没有这个文件夹,必须手动创建)
4. 在 mu-plugins 文件夹内 新建文件:zibll-carousel.php
5. 将以下完整代码复制进去并保存:

“`
php
/**
* Plugin Name: Zibll 文章轮播组件(无标题纯净版)
* 保存位置:/wp-content/mu-plugins/zibll-carousel-notitle.php
*/

if (!defined(‘ABSPATH’)) exit;

class Zibll_Magazine_Carousel_Widget extends WP_Widget {

public function __construct() {
parent::__construct(
‘zibll_magazine_carousel’,
‘【初晨杂志轮播】最新文章’,
array(‘description’ => ‘无标题显示,直接进入轮播内容’)
);
}

private function get_data($instance) {
$post_count = !empty($instance[‘post_count’]) ? intval($instance[‘post_count’]) : 9;
if ($post_count < 3) $post_count = 9; $args = array( 'posts_per_page' => $post_count,
‘post_status’ => ‘publish’,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’
);

if (!empty($instance[‘cat’])) {
$args[‘cat’] = intval($instance[‘cat’]);
}

$posts = get_posts($args);

if (count($posts) < 3) return false; $data = array(); $valid_groups = floor(count($posts) / 3); for ($i = 0; $i < $valid_groups; $i++) { $offset = $i * 3; $data[] = array( 'mAIn’ => $this->fmt($posts[$offset]),
‘side1’ => $this->fmt($posts[$offset + 1]),
‘side2’ => $this->fmt($posts[$offset + 2])
);
}
return $data;
}

private function fmt($post) {
$thumb = get_the_post_thumbnail_url($post->ID, ‘medium_large’);

if (empty($thumb)) {
preg_match(‘/]+src=[\'”]([^\'”]+)[\'”]/i’, $post->post_content, $matches);
$thumb = $matches[1] ?? ”;
}

if (empty($thumb)) {
$colors = array(‘ff6b6b’, ‘4ecdc4′, ’45b7d1′, ’96ceb4’, ‘feca57’);
$color = $colors[$post->ID % count($colors)];
$thumb = “https://via.placeholder.com/800×500/{$color}/ffffff?text=” . urlencode(mb_substr(get_the_title($post), 0, 4));
}

$cat_obj = get_the_category($post->ID);

return array(
‘url’ => get_permalink($post),
‘title’ => get_the_title($post) ?: ‘无标题’,
‘excerpt’ => wp_trim_words($post->post_content, 20, ‘…’),
‘thumb’ => $thumb,
‘cat’ => $cat_obj[0]->name ?? ‘精选’,
‘views’ => function_exists(‘zib_get_pageviews’) ? zib_get_pageviews($post->ID) : rand(100, 999),
‘date’ => human_time_diff(get_the_time(‘U’, $post), current_time(‘timestamp’)) . ‘前’
);
}

public function widget($args, $instance) {
// 获取数据
$data = $this->get_data($instance);

if ($data === false || empty($data)) {
if (current_user_can(‘manage_options’)) {
echo $args[‘before_widget’];
// 注释掉标题输出
// echo $args[‘before_title’] . ‘最新文章’ . $args[‘after_title’];
echo ‘

需要至少3篇文章

‘;
echo $args[‘after_widget’];
}
return;
}

$widget_id = ‘zmc-‘ . $this->number;
$group_count = count($data);

wp_enqueue_style(‘owl-carousel’, ‘https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css’);
wp_enqueue_script(‘owl-carousel’, ‘https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js’, array(‘jquery’), null, true);

echo $args[‘before_widget’];

/* ===== 标题已完全移除 ===== */
// 以下代码被注释掉,不再显示”最新文章”标题
// $title = !empty($instance[‘title’]) ? $instance[‘title’] : ‘最新文章’;
// if ($title) {
// echo $args[‘before_title’] . esc_html($title) . $args[‘after_title’];
// }

?>



1): ?>


全部分类
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容