You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

769 lines
25 KiB

<?php
/**
* Download Center Block Template
* 下载中心区块模板
*
* 支持的参数:
* - id: 区块ID
* - class: CSS类
* - title: 区块标题
* - show_all_tab: 是否显示"ALL"选项卡
* - posts_per_category: 每分类显示文章数
* - order_by: 排序方式
* - order: 排序顺序
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 获取短代码参数或设置默认参数
global $download_center_shortcode_atts;
if (isset($download_center_shortcode_atts) && is_array($download_center_shortcode_atts)) {
$block_id = !empty($download_center_shortcode_atts['id']) ? $download_center_shortcode_atts['id'] : 'nenghui-download-center-' . uniqid();
$block_class = !empty($download_center_shortcode_atts['class']) ? $download_center_shortcode_atts['class'] : '';
$title = !empty($download_center_shortcode_atts['title']) ? $download_center_shortcode_atts['title'] : 'Download Center';
$show_all_tab = !empty($download_center_shortcode_atts['show_all_tab']) ? filter_var($download_center_shortcode_atts['show_all_tab'], FILTER_VALIDATE_BOOLEAN) : true;
$posts_per_category = !empty($download_center_shortcode_atts['posts_per_category']) ? intval($download_center_shortcode_atts['posts_per_category']) : -1;
$enable_pagination = !empty($download_center_shortcode_atts['enable_pagination']) ? filter_var($download_center_shortcode_atts['enable_pagination'], FILTER_VALIDATE_BOOLEAN) : false;
$posts_per_page = !empty($download_center_shortcode_atts['posts_per_page']) ? intval($download_center_shortcode_atts['posts_per_page']) : 6;
$order_by = !empty($download_center_shortcode_atts['order_by']) ? $download_center_shortcode_atts['order_by'] : 'date';
$order = !empty($download_center_shortcode_atts['order']) ? $download_center_shortcode_atts['order'] : 'DESC';
} else {
// 设置默认参数(用于直接调用模板的情况)
$block_id = isset($block_id) ? $block_id : 'nenghui-download-center-' . uniqid();
$block_class = isset($block_class) ? $block_class : '';
$title = isset($title) ? $title : 'Download Center';
$show_all_tab = isset($show_all_tab) ? $show_all_tab : true;
$posts_per_category = isset($posts_per_category) ? $posts_per_category : -1;
$enable_pagination = isset($enable_pagination) ? $enable_pagination : false;
$posts_per_page = isset($posts_per_page) ? $posts_per_page : 6;
$order_by = isset($order_by) ? $order_by : 'date';
$order = isset($order) ? $order : 'DESC';
}
// 获取所有下载分类
$categories = get_terms(array(
'taxonomy' => 'download_category',
'hide_empty' => false,
));
// 如果没有分类,创建默认分类
if (empty($categories) || is_wp_error($categories)) {
$categories = array();
}
// 获取当前页码
$current_page = max(1, get_query_var('paged', 1));
// 根据是否启用分页决定获取数据的方式
if ($enable_pagination) {
// 启用分页时的数据获取
$all_downloads_args = array(
'post_type' => 'download_center',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'paged' => $current_page,
'orderby' => $order_by,
'order' => $order,
);
$all_downloads_query = new WP_Query($all_downloads_args);
$all_downloads = $all_downloads_query->posts;
// 按分类获取下载项目(分页)
$downloads_by_category = array();
$category_queries = array();
foreach ($categories as $category) {
$category_downloads_args = array(
'post_type' => 'download_center',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'paged' => $current_page,
'orderby' => $order_by,
'order' => $order,
'tax_query' => array(
array(
'taxonomy' => 'download_category',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
);
$category_query = new WP_Query($category_downloads_args);
$downloads_by_category[$category->term_id] = $category_query->posts;
$category_queries[$category->term_id] = $category_query;
}
} else {
// 未启用分页时的数据获取(原有逻辑)
$all_downloads_args = array(
'post_type' => 'download_center',
'post_status' => 'publish',
'posts_per_page' => $posts_per_category,
'orderby' => $order_by,
'order' => $order,
);
$all_downloads = get_posts($all_downloads_args);
// 按分类获取下载项目
$downloads_by_category = array();
foreach ($categories as $category) {
$category_downloads_args = array(
'post_type' => 'download_center',
'post_status' => 'publish',
'posts_per_page' => $posts_per_category,
'orderby' => $order_by,
'order' => $order,
'tax_query' => array(
array(
'taxonomy' => 'download_category',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
);
$downloads_by_category[$category->term_id] = get_posts($category_downloads_args);
}
}
// 默认图片路径
$default_image = get_template_directory_uri() . '/assets/images/PDF-IMG.webp';
$download_icon = get_template_directory_uri() . '/assets/images/PDF-ICON.svg';
?>
<section id="<?php echo esc_attr($block_id); ?>" class="download-center-section nenghui-download-center <?php echo esc_attr($block_class); ?>">
<div class="container">
<?php if (!empty($title)): ?>
<div class="section-header">
<h2 class="section-title"><?php echo esc_html($title); ?></h2>
</div>
<?php endif; ?>
<div class="download-center-content">
<!-- 分类选项卡 -->
<div class="download-tabs">
<?php if ($show_all_tab && !empty($all_downloads)): ?>
<button class="tab-button active" data-category="all">
ALL
</button>
<?php endif; ?>
<?php foreach ($categories as $category): ?>
<?php if (!empty($downloads_by_category[$category->term_id])): ?>
<button class="tab-button" data-category="<?php echo esc_attr($category->term_id); ?>">
<?php echo esc_html($category->name); ?>
</button>
<?php endif; ?>
<?php endforeach; ?>
</div>
<!-- 下载内容区域 -->
<div class="download-content">
<!-- 全部下载 -->
<?php if ($show_all_tab && !empty($all_downloads)): ?>
<div class="download-grid active" data-category="all">
<?php foreach ($all_downloads as $download): ?>
<?php
$download_url = get_post_meta($download->ID, '_download_url', true);
$file_size = get_post_meta($download->ID, '_file_size', true);
$file_type = get_post_meta($download->ID, '_file_type', true);
$featured_image = get_the_post_thumbnail_url($download->ID, 'full');
$image_url = $featured_image ? $featured_image : $default_image;
// 如果没有设置下载链接,使用特色图像作为下载链接
if (empty($download_url) && $featured_image) {
$download_url = $featured_image;
}
?>
<div class="download-card">
<div class="card-image">
<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($download->post_title); ?>" loading="lazy">
</div>
<div class="card-content">
<h3 class="card-title"><?php echo esc_html($download->post_title); ?></h3>
<?php if (!empty($download->post_excerpt)): ?>
<p class="card-excerpt"><?php echo esc_html($download->post_excerpt); ?></p>
<?php endif; ?>
<div class="card-meta">
<?php if (!empty($file_type)): ?>
<span class="file-type"><?php echo esc_html($file_type); ?></span>
<?php endif; ?>
<?php if (!empty($file_size)): ?>
<span class="file-size"><?php echo esc_html($file_size); ?></span>
<?php endif; ?>
</div>
<?php if (!empty($download_url)): ?>
<a href="<?php echo esc_url($download_url); ?>" class="download-button" download target="_blank">
<img src="<?php echo esc_url($download_icon); ?>" alt="Download" class="download-icon">
Download
</a>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<!-- 分类下载 -->
<?php foreach ($categories as $category): ?>
<?php if (!empty($downloads_by_category[$category->term_id])): ?>
<div class="download-grid" data-category="<?php echo esc_attr($category->term_id); ?>">
<?php foreach ($downloads_by_category[$category->term_id] as $download): ?>
<?php
$download_url = get_post_meta($download->ID, '_download_url', true);
$file_size = get_post_meta($download->ID, '_file_size', true);
$file_type = get_post_meta($download->ID, '_file_type', true);
$featured_image = get_the_post_thumbnail_url($download->ID, 'full');
$image_url = $featured_image ? $featured_image : $default_image;
// 如果没有设置下载链接,使用特色图像作为下载链接
if (empty($download_url) && $featured_image) {
$download_url = $featured_image;
}
?>
<div class="download-card">
<div class="card-image">
<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($download->post_title); ?>" loading="lazy">
</div>
<div class="card-content">
<h3 class="card-title"><?php echo esc_html($download->post_title); ?></h3>
<?php if (!empty($download->post_excerpt)): ?>
<p class="card-excerpt"><?php echo esc_html($download->post_excerpt); ?></p>
<?php endif; ?>
<div class="card-meta">
<?php if (!empty($file_type)): ?>
<span class="file-type"><?php echo esc_html($file_type); ?></span>
<?php endif; ?>
<?php if (!empty($file_size)): ?>
<span class="file-size"><?php echo esc_html($file_size); ?></span>
<?php endif; ?>
</div>
<?php if (!empty($download_url)): ?>
<a href="<?php echo esc_url($download_url); ?>" class="download-button" download target="_blank">
<img src="<?php echo esc_url($download_icon); ?>" alt="Download" class="download-icon">
Download
</a>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php if ($enable_pagination): ?>
<!-- 分页导航 -->
<div class="download-pagination">
<!-- 全部分类的分页 -->
<?php if ($show_all_tab && !empty($all_downloads)): ?>
<div class="pagination-wrapper" data-category="all">
<?php
$pagination_args = array(
'total' => $all_downloads_query->max_num_pages,
'current' => $current_page,
'format' => '?paged=%#%',
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
'type' => 'array'
);
$pagination_links = paginate_links($pagination_args);
if ($pagination_links) {
echo '<nav class="pagination-nav">';
echo '<ul class="pagination-list">';
foreach ($pagination_links as $link) {
echo '<li class="pagination-item">' . $link . '</li>';
}
echo '</ul>';
echo '</nav>';
}
?>
</div>
<?php endif; ?>
<!-- 各分类的分页 -->
<?php foreach ($categories as $category): ?>
<?php if (!empty($downloads_by_category[$category->term_id]) && isset($category_queries[$category->term_id])): ?>
<div class="pagination-wrapper" data-category="<?php echo esc_attr($category->term_id); ?>">
<?php
$category_query = $category_queries[$category->term_id];
$pagination_args = array(
'total' => $category_query->max_num_pages,
'current' => $current_page,
'format' => '?paged=%#%',
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
'type' => 'array'
);
$pagination_links = paginate_links($pagination_args);
if ($pagination_links) {
echo '<nav class="pagination-nav">';
echo '<ul class="pagination-list">';
foreach ($pagination_links as $link) {
echo '<li class="pagination-item">' . $link . '</li>';
}
echo '</ul>';
echo '</nav>';
}
?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</section>
<style>
.nenghui-download-center {
padding: 60px 0;
background: #f8f9fa;
}
.nenghui-download-center .container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
.nenghui-download-center .section-header {
text-align: center;
margin-bottom: 50px;
}
.nenghui-download-center .section-title {
font-size: 2.5rem;
font-weight: 700;
color: #2c3e50;
margin: 0;
position: relative;
display: inline-block;
}
.nenghui-download-center .section-title::after {
content: '';
position: absolute;
bottom: -20px;
left: 50%;
transform: translateX(-50%);
width: 60px;
height: 3px;
background: linear-gradient(135deg, #2cb5a9, #00699f);
border-radius: 2px;
}
/* 选项卡样式 */
.download-tabs {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 40px;
flex-wrap: wrap;
}
.tab-button {
padding: 12px 24px;
border: 2px solid #e0e0e0;
background: #fff;
color: #666;
font-size: 16px;
font-weight: 500;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.tab-button:hover {
border-color: #2cb5a9;
color: #2cb5a9;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(44, 181, 169, 0.2);
transition: all 0.3s ease;
}
.tab-button.active {
background: linear-gradient(135deg, #2cb5a9, #00699f);
border-color: #2cb5a9;
color: #fff;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(44, 181, 169, 0.3);
}
/* 下载网格布局 */
.download-grid {
display: none;
grid-template-columns: repeat(5, 1fr);
gap: 30px;
animation: fadeIn 0.5s ease;
}
.download-grid.active {
display: grid;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* 下载卡片样式 */
.download-card {
background: #fff;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
border: 1px solid #f0f0f0;
}
.download-card:hover {
transform: translateY(-8px);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
border-color: #2cb5a9;
transition: all 0.3s ease;
}
.card-image {
position: relative;
overflow: hidden;
height: 200px;
background: #f8f9fa;
}
.card-image img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.3s ease;
}
.download-card:hover .card-image img {
transform: scale(1.05);
transition: all 0.3s ease;
}
.card-content {
padding: 20px;
}
.card-title {
font-size: 1rem;
font-weight: 600;
color: #2c3e50;
margin: 0 0 10px 0;
line-height: 1.5;
height: 3em;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
word-wrap: break-word;
word-break: break-word;
white-space: normal;
}
.card-excerpt {
color: #666;
font-size: 14px;
line-height: 1.5;
margin: 0 0 15px 0;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
.card-meta {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.file-type,
.file-size {
padding: 4px 8px;
background: #f0f8ff;
color: #2cb5a9;
font-size: 12px;
font-weight: 500;
border-radius: 4px;
text-transform: uppercase;
}
.download-button {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 12px 20px;
background: linear-gradient(135deg, #2cb5a9, #00699f);
color: #fff;
text-decoration: none;
border-radius: 8px;
font-weight: 500;
font-size: 14px;
transition: all 0.3s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
width: 100%;
justify-content: center;
}
.download-button:hover {
background: linear-gradient(135deg, #00699f, #2cb5a9);
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(44, 181, 169, 0.3);
color: #fff;
text-decoration: none;
transition: all 0.3s ease;
}
.download-icon {
width: 16px;
height: 16px;
filter: brightness(0) invert(1);
}
/* 分页样式 */
.download-pagination {
margin-top: 40px;
}
.pagination-wrapper {
display: none;
}
.pagination-wrapper.active {
display: block;
}
.pagination-nav {
display: flex;
justify-content: center;
}
.pagination-list {
display: flex;
list-style: none;
margin: 0;
padding: 0;
gap: 8px;
align-items: center;
}
.pagination-item {
margin: 0;
}
.pagination-item a,
.pagination-item span {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 40px;
height: 40px;
padding: 8px 12px;
border: 1px solid #e0e0e0;
background: #fff;
color: #666;
text-decoration: none;
border-radius: 6px;
font-size: 14px;
font-weight: 500;
transition: all 0.3s ease;
}
.pagination-item a:hover {
border-color: #2cb5a9;
background: #2cb5a9;
color: #fff;
transform: translateY(-1px);
box-shadow: 0 2px 8px rgba(44, 181, 169, 0.2);
transition: all 0.3s ease;
}
.pagination-item .current {
background: linear-gradient(135deg, #2cb5a9, #00699f);
border-color: #2cb5a9;
color: #fff;
font-weight: 600;
}
.pagination-item .dots {
border: none;
background: transparent;
color: #999;
cursor: default;
}
.pagination-item .prev,
.pagination-item .next {
padding: 8px 16px;
font-size: 13px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.pagination-item .prev:hover,
.pagination-item .next:hover {
background: linear-gradient(135deg, #2cb5a9, #00699f);
transition: all 0.3s ease;
}
/* 响应式设计 */
@media (max-width: 1200px) {
.download-grid {
grid-template-columns: repeat(4, 1fr);
gap: 25px;
}
}
@media (max-width: 992px) {
.download-grid {
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
}
@media (max-width: 768px) {
.nenghui-download-center {
padding: 40px 0;
}
.nenghui-download-center .container {
padding: 0 15px;
}
.nenghui-download-center .section-title {
font-size: 2rem;
}
.download-tabs {
gap: 8px;
margin-bottom: 30px;
}
.tab-button {
padding: 10px 16px;
font-size: 14px;
}
.card-content {
padding: 15px;
}
.card-title {
font-size: 0.95rem;
line-height: 1.4;
height: 2.8em;
}
.pagination-item a,
.pagination-item span {
min-width: 36px;
height: 36px;
padding: 6px 10px;
font-size: 13px;
}
.pagination-item .prev,
.pagination-item .next {
padding: 6px 12px;
font-size: 12px;
}
}
@media (max-width: 480px) {
.download-grid {
grid-template-columns: repeat(2, 1fr);
gap: 15px;
}
.tab-button {
padding: 8px 12px;
font-size: 13px;
}
.pagination-list {
gap: 4px;
}
.pagination-item a,
.pagination-item span {
min-width: 32px;
height: 32px;
padding: 4px 8px;
font-size: 12px;
}
.pagination-item .prev,
.pagination-item .next {
padding: 4px 8px;
font-size: 11px;
}
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
const tabButtons = document.querySelectorAll('#<?php echo esc_js($block_id); ?> .tab-button');
const downloadGrids = document.querySelectorAll('#<?php echo esc_js($block_id); ?> .download-grid');
const paginationWrappers = document.querySelectorAll('#<?php echo esc_js($block_id); ?> .pagination-wrapper');
tabButtons.forEach(button => {
button.addEventListener('click', function() {
const category = this.getAttribute('data-category');
// 移除所有活动状态
tabButtons.forEach(btn => btn.classList.remove('active'));
downloadGrids.forEach(grid => grid.classList.remove('active'));
paginationWrappers.forEach(wrapper => wrapper.classList.remove('active'));
// 添加当前活动状态
this.classList.add('active');
const targetGrid = document.querySelector('#<?php echo esc_js($block_id); ?> .download-grid[data-category="' + category + '"]');
if (targetGrid) {
targetGrid.classList.add('active');
}
// 显示对应的分页组件
const targetPagination = document.querySelector('#<?php echo esc_js($block_id); ?> .pagination-wrapper[data-category="' + category + '"]');
if (targetPagination) {
targetPagination.classList.add('active');
}
});
});
// 初始化时显示第一个活动选项卡对应的分页
const activeTab = document.querySelector('#<?php echo esc_js($block_id); ?> .tab-button.active');
if (activeTab) {
const category = activeTab.getAttribute('data-category');
const targetPagination = document.querySelector('#<?php echo esc_js($block_id); ?> .pagination-wrapper[data-category="' + category + '"]');
if (targetPagination) {
targetPagination.classList.add('active');
}
}
});
</script>