40, 'width' => 160, 'flex-height' => true, )); add_theme_support('html5', array( 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script', )); register_nav_menus(array( 'primary' => '主导航', )); } // ── 资源加载 ────────────────────────────────── add_action('wp_enqueue_scripts', 'kjweji_enqueue_assets'); function kjweji_enqueue_assets() { wp_enqueue_style('kjweji-style', get_stylesheet_uri(), array(), '1.0.0'); // 字体改用系统自带(Google Fonts 在中国大陆不可用) wp_enqueue_script('kjweji-lightbox', get_template_directory_uri() . '/assets/js/lightbox.js', array(), '1.0.0', true); } // ── 注册自定义文章类型: tucao(微言) ────────── add_action('init', 'kjweji_register_tucao'); function kjweji_register_tucao() { register_post_type('tucao', array( 'labels' => array( 'name' => '微言', 'singular_name' => '微言', 'add_new' => '写微言', 'add_new_item' => '写一条微言', 'edit_item' => '编辑微言', 'new_item' => '新微言', 'view_item' => '查看微言', 'search_items' => '搜索微言', 'not_found' => '暂无微言', 'not_found_in_trash' => '回收站中暂无微言', ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'tucao'), 'show_in_rest' => true, 'supports' => array('title', 'editor', 'author', 'thumbnail'), 'menu_position' => 25, 'menu_icon' => 'dashicons-format-chat', )); } // ── 摘要取前 2-3 句 ─────────────────────────── // 摘要:取文章内容的前 N 个字符(在句号处截断) function kjweji_excerpt($max_sentences = 5, $max_chars = 300) { $content = get_the_content(); $content = wp_strip_all_tags($content); if (empty($content)) return ''; // 先按字符截断 if (mb_strlen($content) > $max_chars) { $content = mb_substr($content, 0, $max_chars); // 回退到最后一个句号 $last_period = mb_strrpos($content, '。'); if ($last_period !== false && $last_period > $max_chars / 2) { $content = mb_substr($content, 0, $last_period + 1); } } return esc_html(trim($content)); } // ── OG 标签 ─────────────────────────────────── add_action('wp_head', 'kjweji_og_tags'); function kjweji_og_tags() { if (is_singular()) { $title = get_the_title(); $desc = kjweji_excerpt(2); $url = get_permalink(); } else { $title = get_bloginfo('name'); $desc = get_bloginfo('description'); $url = home_url(); } echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; if (is_singular() && has_post_thumbnail()) { $img = get_the_post_thumbnail_url(null, 'kjweji-featured'); echo '' . "\n"; } echo '' . "\n"; } // ── 首页只显示普通文章(非 tucao) ───────────── add_action('pre_get_posts', 'kjweji_home_only_posts'); function kjweji_home_only_posts($query) { if (!is_admin() && $query->is_main_query() && $query->is_home()) { $query->set('post_type', 'post'); } } // 浏览量 function kjweji_get_views() { $count = get_post_meta(get_the_ID(), 'post_views', true); return $count ? (int) $count : 0; } // 禁用 WP emoji 转图片 remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('wp_print_styles', 'print_emoji_styles'); remove_action('admin_print_scripts', 'print_emoji_detection_script'); remove_action('admin_print_styles', 'print_emoji_styles'); add_filter('emoji_svg_url', '__return_false'); // 评论表单中文化 add_filter('comment_form_default_fields', function($fields) { $fields['author'] = '

'; $fields['email'] = '

'; $fields['url'] = '

'; $fields['cookies'] = ''; return $fields; }); add_action('template_include', function($template) { if (get_query_var('post_type') === 'post' && !is_home() && !is_singular()) { $t = locate_template('archive.php'); if ($t) return $t; } return $template; }); // 本地头像(团队证件照) add_filter('get_avatar', function($avatar, $id_or_email, $size, $default, $alt) { $uid = 0; if (is_numeric($id_or_email)) $uid = (int) $id_or_email; elseif (is_object($id_or_email) && isset($id_or_email->user_id)) $uid = (int) $id_or_email->user_id; $att_id = get_user_meta($uid, 'custom_avatar', true); if ($att_id) { $url = wp_get_attachment_image_url($att_id, 'medium'); if ($url) { return ''; } } return $avatar; // fallback }, 10, 5); // 激活主题时自动初始化 add_action('after_switch_theme', 'kjweji_setup_pages'); function kjweji_setup_pages() { // 创建笺文页 if (!get_page_by_path('articles')) { wp_insert_post([ 'post_title' => '笺文', 'post_name' => 'articles', 'post_type' => 'page', 'post_status' => 'publish', 'meta_input' => ['_wp_page_template' => 'page-articles.php'], ]); } // 创建拾影页 if (!get_page_by_path('photos')) { wp_insert_post([ 'post_title' => '拾影', 'post_name' => 'photos', 'post_type' => 'page', 'post_status' => 'publish', 'meta_input' => ['_wp_page_template' => 'page-photos.php'], ]); } // 创建导航菜单 if (!wp_get_nav_menu_object('主导航')) { $menu_id = wp_create_nav_menu('主导航'); foreach ([ ['首页', '/'], ['笺文', '/articles/'], ['微言', '/tucao/'], ['拾影', '/photos/'], ] as $item) { wp_update_nav_menu_item($menu_id, 0, [ 'menu-item-title' => $item[0], 'menu-item-url' => $item[1], 'menu-item-status' => 'publish', ]); } $locations = get_theme_mod('nav_menu_locations') ?: []; $locations['primary'] = $menu_id; set_theme_mod('nav_menu_locations', $locations); } flush_rewrite_rules(); } // 自定义文章类型:拾影 add_action('init', 'kjweji_register_photo'); function kjweji_register_photo() { register_post_type('photo', array( 'labels' => array( 'name' => '拾影', 'singular_name' => '拾影', 'add_new' => '发拾影', 'add_new_item' => '发新拾影', 'edit_item' => '编辑拾影', ), 'public' => true, 'has_archive' => false, 'rewrite' => array('slug' => 'photo/%post_id%'), 'show_in_rest' => true, 'supports' => array('title', 'editor', 'author', 'thumbnail'), 'menu_position' => 26, 'menu_icon' => 'dashicons-format-gallery', )); } add_filter('post_type_link', function($link, $post) { if ($post->post_type === 'photo' && $post->post_status === 'publish') { return home_url(user_trailingslashit('/photo/' . $post->ID . '/' . $post->post_name)); } return $link; }, 10, 2); // 匹配 /photo/123/slug/ 格式 add_action('init', function() { add_rewrite_rule('^photo/([0-9]+)/([^/]+)/?$', 'index.php?post_type=photo&p=$matches[1]', 'top'); add_rewrite_rule('^photo/([0-9]+)/?$', 'index.php?post_type=photo&p=$matches[1]', 'top'); });