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 '

' . esc_html($args['title']) . '

暂无相关文章

'; return; } // 获取分类链接 $category_link = get_category_link($args['category_id']); ?>