// ============================================================
// ⚙️ 설정
// ============================================================
define('KRM_CALENDAR_CAT_ID', 2378);
define('KRM_TARGET_CAT_IDS', [8, 17, 18]);
define('KRM_EXCERPT_LENGTH', 200);
// ============================================================
// 🚀 기사 발행시 자동 실행
// ============================================================
add_action('publish_post', 'krm_add_to_calendar', 10, 2);
function krm_add_to_calendar($post_id, $post) {
if (has_category(KRM_CALENDAR_CAT_ID, $post_id)) return;
if (has_tag('flash', $post_id)) return;
$post_cats = wp_get_post_categories($post_id);
$is_target = array_intersect($post_cats, KRM_TARGET_CAT_IDS);
if (empty($is_target)) return;
$calendar_post_id = krm_get_or_create_calendar_post();
if (!$calendar_post_id) return;
$block = krm_build_article_block($post_id, $post);
$calendar_post = get_post($calendar_post_id);
$new_content = $block . "\n\n" . $calendar_post->post_content;
wp_update_post([
'ID' => $calendar_post_id,
'post_content' => $new_content,
]);
}
// ============================================================
// 📅 오늘 뉴스 캘린더 포스트 가져오거나 생성
// ============================================================
function krm_get_or_create_calendar_post() {
$today = date('Y-m-d');
$date_from = $today . 'T00:00:00';
$date_to = $today . 'T23:59:59';
$existing = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => [KRM_CALENDAR_CAT_ID],
'date_query' => [
['after' => $date_from, 'before' => $date_to, 'inclusive' => true]
],
'posts_per_page' => 1,
]);
if (!empty($existing)) {
return $existing[0]->ID;
}
$year = date('Y');
$month = (int)date('m');
$day = (int)date('d');
$title = "{$year}년 {$month}월 {$day}일 뉴스 캘린더";
$new_post_id = wp_insert_post([
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_category' => [KRM_CALENDAR_CAT_ID],
'post_author' => 1,
]);
return $new_post_id ?: null;
}
// ============================================================
// 🏗️ 일반 기사 블록 HTML 생성 (JPOST 스타일)
// ============================================================
function krm_build_article_block($post_id, $post) {
$time = get_the_date('H:i', $post_id);
$title = get_the_title($post_id);
$permalink = get_permalink($post_id);
$thumb_html = '';
if (has_post_thumbnail($post_id)) {
$thumb_url = get_the_post_thumbnail_url($post_id, 'medium');
$thumb_html = '
'
. '
 . ')
'
. '
';
}
$raw_content = wp_strip_all_tags($post->post_content);
$excerpt = mb_substr($raw_content, 0, KRM_EXCERPT_LENGTH);
if (mb_strlen($raw_content) > KRM_EXCERPT_LENGTH) {
$excerpt .= '...';
}
$block = '
';
$block .= '
' . esc_html($time) . '
';
$block .= '
';
$block .= $thumb_html;
$block .= '
' . esc_html($excerpt) . '
';
$block .= '
기사 전문 보기 →';
$block .= '
';
return $block;
}