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.

120 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* Social Activities Block Template
* 社会活动区块模板
*
* @package Nenghui Energy Theme
* @since 1.0.0
*
* @param array $args {
* Optional. Array of arguments.
* @type int $category_id 文章分类ID默认为8
* @type int $posts_count 显示文章数量默认为3
* @type string $title 区块标题,默认为"社会责任"
* }
*/
// 设置默认参数
$defaults = array(
'category_id' => 8,
'posts_count' => 3,
'title' => '社会责任'
);
$args = wp_parse_args($args ?? array(), $defaults);
// 构建查询参数
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $args['posts_count'],
'orderby' => 'date',
'order' => 'DESC',
'cat' => intval($args['category_id'])
);
// 尝试获取缓存
$cache_key = 'social_activities_' . md5(serialize($query_args) . (function_exists('get_theme_cache_version') ? get_theme_cache_version() : ''));
$posts = get_transient($cache_key);
if ($posts === false) {
// 执行查询
$social_activities_query = new WP_Query($query_args);
$posts = $social_activities_query->posts;
// 如果有文章设置缓存缓存1小时
if (!empty($posts)) {
set_transient($cache_key, $posts, 3600);
}
}
// 如果没有文章,显示提示信息
if (empty($posts)) {
wp_reset_postdata();
echo '<div class="social-activities-container"><div class="social-activities-header"><h2>' . esc_html($args['title']) . '</h2></div><div class="no-posts-message"><p>暂无相关文章</p></div></div>';
return;
}
// 获取分类链接
$category_link = get_category_link($args['category_id']);
?>
<div class="social-activities-container">
<div class="social-activities-header">
<h2><?php echo esc_html($args['title']); ?></h2>
</div>
<div class="social-activities-grid">
<?php foreach ($posts as $post): ?>
<?php setup_postdata($post); ?>
<div class="social-activity-card">
<div class="activity-image">
<?php if (has_post_thumbnail($post->ID)): ?>
<img src="<?php echo esc_url(get_the_post_thumbnail_url($post->ID, 'medium')); ?>"
alt="<?php echo esc_attr(get_the_title($post->ID)); ?>"
loading="lazy">
<?php else: ?>
<div class="placeholder-image">
<span>暂无图片</span>
</div>
<?php endif; ?>
</div>
<div class="activity-content">
<h3 class="activity-title">
<a href="<?php echo esc_url(get_permalink($post->ID)); ?>">
<?php echo esc_html(get_the_title($post->ID)); ?>
</a>
</h3>
<div class="activity-excerpt">
<?php
$excerpt = get_the_excerpt($post->ID);
if (empty($excerpt)) {
$excerpt = wp_trim_words(strip_tags($post->post_content), 20, '...');
}
echo esc_html($excerpt);
?>
</div>
<div class="activity-meta">
<span class="activity-date">
<?php echo esc_html(get_the_date('Y.m.d', $post->ID)); ?>
</span>
<a href="<?php echo esc_url(get_permalink($post->ID)); ?>" class="activity-link">
查看详情 →
</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="social-activities-footer">
<a href="<?php echo esc_url($category_link); ?>" class="view-more-btn">
查看更多 →
</a>
</div>
</div>
<?php
// 重置全局 $post 对象
wp_reset_postdata();
?>