教程中心

分类页面代码

右栏pc端全局统一布局,手机端自适应

 

<?php
/*
@name 双栏分类模板(右栏PC端最大化)
@description 所有分类右栏在PC端同等大小最大化,手机端自适应
*/
defined('EMLOG_ROOT') || exit('access denied!');
?>

<!-- 引入图标库 -->
<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">

<style>
/* 全局布局容器 */
.category-container {
    display: flex;
    flex-wrap: wrap;
    gap: 25px;
    max-width: 1240px;
    margin: 30px auto;
    padding: 0 20px;
    width: 100%; /* 确保容器占满可用宽度 */
}

/* 左栏:固定宽度,确保右栏空间一致 */
.xianyida-sidebar {
    flex: 0 0 260px; /* 固定宽度260px,不伸缩 */
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    overflow: hidden;
}

/* 左栏标题栏 */
.xianyida-sidebar-header {
    background: #2c76d2;
    color: #fff;
    padding: 14px 20px;
    font-size: 16px;
    font-weight: 500;
    display: flex;
    align-items: center;
}
.xianyida-sidebar-header i {
    margin-right: 8px;
    font-size: 18px;
}

/* 左栏分类列表 */
.xianyida-category-list {
    list-style: none;
    margin: 0;
    padding: 0;
}
.xianyida-category-item {
    border-bottom: 1px solid #f5f5f5;
}
.xianyida-category-link {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    color: #333;
    text-decoration: none;
    font-size: 14px;
    transition: background 0.2s ease;
}
.xianyida-category-link:hover {
    background: #fafafa;
}
.xianyida-category-link.active {
    background: #f0f7ff;
    color: #2c76d2;
    font-weight: 500;
}
.xianyida-category-count {
    background: #f5f5f5;
    color: #666;
    font-size: 12px;
    padding: 2px 8px;
    border-radius: 10px;
}

/* 右栏:PC端最大化显示,所有分类保持一致大小 */
.category-main {
    flex: 1 1 calc(100% - 285px); /* 计算剩余空间,确保填满 */
    min-width: 0; /* 关键:允许内容在弹性容器中收缩 */
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    padding: 25px;
    width: 100%; /* 确保占满分配的空间 */
    box-sizing: border-box; /* 确保内边距不影响整体宽度 */
}

/* 文章卡片样式 */
.article-card {
    border: 1px solid #f1f1f1;
    border-radius: 8px;
    padding: 18px;
    margin-bottom: 16px;
    transition: all 0.3s ease;
}
.article-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}
.article-title {
    font-size: 18px;
    font-weight: 500;
    margin-bottom: 8px;
    line-height: 1.4;
}
.article-title a {
    color: #333;
    text-decoration: none;
}
.article-title a:hover {
    color: #2c76d2;
}
.article-excerpt {
    color: #666;
    font-size: 14px;
    margin-bottom: 10px;
    line-height: 1.6;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.article-meta {
    font-size: 13px;
    color: #999;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

/* 分页样式 */
.pagination {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-top: 30px;
}
.pagination a,
.pagination span {
    display: inline-block;
    padding: 6px 12px;
    border: 1px solid #eee;
    border-radius: 4px;
    color: #666;
    text-decoration: none;
    font-size: 14px;
    transition: all 0.2s ease;
}
.pagination a:hover {
    border-color: #2c76d2;
    color: #2c76d2;
}
.pagination .current {
    background: #2c76d2;
    color: #fff;
    border-color: #2c76d2;
}

/* 手机端响应式设置 */
@media (max-width: 768px) {
    .category-container {
        flex-direction: column; /* 堆叠布局 */
        gap: 15px;
        margin: 15px auto;
        padding: 0 15px;
    }
    .xianyida-sidebar,
    .category-main {
        flex: 1 1 100% !important; /* 占满屏幕宽度 */
        width: 100% !important;
    }
    .xianyida-sidebar {
        max-width: 100%; /* 确保在小屏幕不溢出 */
    }
    .category-main {
        padding: 18px;
    }
}

/* 平板设备适配 */
@media (min-width: 769px) and (max-width: 1024px) {
    .xianyida-sidebar {
        flex: 0 0 220px; /* 平板上适当缩小左栏 */
    }
    .category-main {
        flex: 1 1 calc(100% - 245px); /* 相应调整右栏宽度 */
    }
}
</style>

<div class="category-container">
    <!-- 左栏:分类列表 -->
    <div class="xianyida-sidebar">
        <div class="xianyida-sidebar-header">
            <i class="fa fa-folder-open"></i>
            全部分类
        </div>
        <ul class="xianyida-category-list">
            <?php
            // 获取所有分类数据
            if (function_exists('getAllSorts')) {
                $all_sorts = getAllSorts();
            } else {
                $CACHE = Cache::getInstance();
                $all_sorts = $CACHE->readCache('sort');
            }
            // 遍历分类
            foreach ($all_sorts as $sort) {
                $is_active = $sort['sid'] == $sortid ? 'active' : '';
                $sort_url = Url::sort($sort['sid']);
                $sort_name = $sort['sortname'];
                $sort_count = $sort['lognum'];
            ?>
            <li class="xianyida-category-item">
                <a href="<?= $sort_url ?>" class="xianyida-category-link <?= $is_active ?>">
                    <span><?= $sort_name ?></span>
                    <span class="xianyida-category-count"><?= $sort_count ?></span>
                </a>
            </li>
            <?php } ?>
        </ul>
    </div>

    <!-- 右栏:文章列表(所有分类下保持同等大小) -->
    <div class="category-main">
        <?php
        // 分页处理
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $perpage = Option::get('index_log') ?: 10;
        $start = ($page - 1) * $perpage;

        // 数据库查询
        $DB = MySql::getInstance();
        $sql = "SELECT * FROM " . DB_PREFIX . "blog 
                WHERE sortid = {$sortid} 
                AND type = 'blog' 
                AND hide = 'n' 
                AND checked = 'y' 
                ORDER BY top DESC, date DESC 
                LIMIT {$start}, {$perpage}";
        $logs = $DB->query($sql);

        // 总文章数查询
        $total_sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog 
                     WHERE sortid = {$sortid} 
                     AND type = 'blog' 
                     AND hide = 'n' 
                     AND checked = 'y'";
        $total = $DB->once_fetch_array($total_sql)['total'] ?? 0;
        $total_pages = ceil($total / $perpage);
        ?>

        <?php if ($total == 0): ?>
            <div style="text-align: center; padding: 40px 0; color: #999;">
                <i class="fa fa-file-text-o" style="font-size: 36px; margin-bottom: 12px;"></i>
                <p>该分类下暂无文章</p>
            </div>
        <?php else: ?>
            <?php while ($log = $DB->fetch_array($logs)): ?>
                <div class="article-card">
                    <!-- 文章标题 -->
                    <h3 class="article-title">
                        <a href="<?= Url::log($log['gid']) ?>" title="<?= $log['title'] ?>">
                            <?= $log['title'] ?>
                        </a>
                    </h3>

                    <!-- 标签过滤+摘要 -->
                    <div class="article-excerpt">
                        <?php
                        $original_content = $log['content'];
                        // 过滤标签
                        $filter_rules = [
                            '/\[lv(?:\s+[^]]+)?\](.*?)\[\/lv\]/is',
                            '/\[cv(?:\s+[^]]+)?\](.*?)\[\/cv\]/is',
                            '/<code(?:\s+[^>]+)?>.*?<\/code>/is',
                            '/<p\s*\/?>|<br\s*\/?>/is',
                            '/\s+/',
                        ];
                        $clean_content = preg_replace($filter_rules, ' ', $original_content);
                        // 生成摘要
                        if (!empty($log['excerpt'])) {
                            $excerpt = $log['excerpt'];
                        } else {
                            $pure_text = trim(strip_tags($clean_content));
                            $excerpt = mb_substr($pure_text, 0, 100, 'utf-8');
                        }
                        echo $excerpt . (mb_strlen(trim(strip_tags($clean_content))) > 100 ? '...' : '');
                        ?>
                    </div>

                    <!-- 文章元信息 -->
                    <div class="article-meta">
                        <span><i class="fa fa-calendar"></i><?= gmdate('Y-m-d', $log['date']) ?></span>
                        <span><i class="fa fa-eye"></i><?= $log['views'] ?></span>
                        <span><i class="fa fa-comment"></i><?= $log['comnum'] ?></span>
                        <?php editflg($log['gid'], $log['author']); ?>
                    </div>
                </div>
            <?php endwhile; ?>

            <!-- 分页 -->
            <?php if ($total_pages > 1): ?>
                <div class="pagination">
                    <?= pagination($total_pages, $page, Url::sort($sortid)) ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>

<?php include View::getView('footer'); ?>