// 插入内容到文章中 add_filter('the_content', 'cci_insert_content'); function cci_insert_content($content) { if (is_single()) { $options = get_option('cci_options'); // 获取当前时间 $current_time = current_time('Y-m-d H:i:s'); // 获取绝对时间 $start_time = isset($options['cci_start_time']) ? $options['cci_start_time'] : ''; $end_time = isset($options['cci_end_time']) ? $options['cci_end_time'] : ''; // 获取相对时间 $relative_time_value = isset($options['cci_relative_time']) ? (int)$options['cci_relative_time'] : 0; $relative_time_unit = isset($options['cci_relative_time_unit']) ? $options['cci_relative_time_unit'] : 'days'; // 计算相对时间的生效时间 $effective_start_time = new DateTime($current_time); if ($relative_time_value > 0) { switch ($relative_time_unit) { case 'minutes': $effective_start_time->modify("+$relative_time_value minutes"); break; case 'hours': $effective_start_time->modify("+$relative_time_value hours"); break; case 'days': $effective_start_time->modify("+$relative_time_value days"); break; case 'weeks': $effective_start_time->modify("+$relative_time_value weeks"); break; case 'months': $effective_start_time->modify("+$relative_time_value months"); break; } $start_time = $effective_start_time->format('Y-m-d H:i:s'); } // 输出调试信息 error_log("当前时间: $current_time"); error_log("生效时间: $start_time"); error_log("结束时间: $end_time"); // 检查时间有效性 if ($current_time < $start_time || ($end_time !== '' && $current_time > $end_time)) { error_log("时间不在有效范围内。"); return $content; // 如果时间不在范围内,直接返回内容 } // 在文章顶部插入内容(如果启用) if (isset($options['top_content_enabled']) && $options['top_content_enabled']) { $content = $options['cci_top_content'] . $content; } // 在指定段落后插入中部内容 if (isset($options['cci_ad_after_paragraph']) && intval($options['cci_ad_after_paragraph']) > 0) { $ad_after_paragraph = intval($options['cci_ad_after_paragraph']); $paragraphs = explode('

', $content); if (count($paragraphs) > $ad_after_paragraph) { array_splice($paragraphs, $ad_after_paragraph, 0, [$options['cci_middle_content']]); $content = implode('

', $paragraphs); } } // 检查内部推荐文章的时间限制 $recommended_posts = cci_insert_recommended_posts($options); if (!empty($recommended_posts)) { $content .= $recommended_posts; } else { error_log("没有找到推荐文章。"); } // 检查外部推荐文章的时间 if ($current_time >= $options['cci_external_start_time'] && $current_time <= $options['cci_external_end_time']) { $content .= cci_insert_external_links($options); } // 在底部插入内容(如果启用) if (isset($options['bottom_content_enabled']) && $options['bottom_content_enabled']) { $content .= $options['cci_bottom_content']; } } return $content; } // 内部推荐文章插入逻辑 function cci_insert_recommended_posts($options) { $current_post_id = get_the_ID(); $post_ids = isset($options['cci_recommend_post_ids']) ? $options['cci_recommend_post_ids'] : ''; $post_ids_array = array_unique(array_map('trim', explode(',', $post_ids))); // 输出推荐文章 ID error_log("推荐文章 ID: " . implode(', ', $post_ids_array)); // 从推荐文章ID中排除当前文章ID $post_ids_array = array_diff($post_ids_array, [$current_post_id]); if (!empty($post_ids_array)) { $recommended_posts = new WP_Query([ 'post__in' => $post_ids_array, 'orderby' => 'post__in', 'posts_per_page' => -1, ]); if ($recommended_posts->have_posts()) { $title_font_size = isset($options['cci_title_font_size']) ? esc_attr($options['cci_title_font_size']) : '16px'; $title_color = isset($options['cci_title_color']) ? esc_attr($options['cci_title_color']) : '#000000'; $output = '

' . esc_html($options['cci_recommend_title']) . '

'; wp_reset_postdata(); return $output; } else { error_log("推荐文章查询没有结果。"); } } else { error_log("内部推荐文章 ID 为空或无效。"); } return ''; }
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/kbky.net/wp-content/plugins/custom-content-plus/custom-content-plus.php:1) in /www/wwwroot/kbky.net/wp-includes/pluggable.php on line 1435

Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/kbky.net/wp-content/plugins/custom-content-plus/custom-content-plus.php:1) in /www/wwwroot/kbky.net/wp-includes/pluggable.php on line 1438