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; elseif (is_object($id_or_email) && $id_or_email instanceof WP_Comment) { $uid = (int) $id_or_email->user_id; } // 如果传的是 email 但找不到用户,尝试用 email 找 if (!$uid && is_string($id_or_email) && strpos($id_or_email, '@') !== false) { $u = get_user_by('email', $id_or_email); if ($u) $uid = $u->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 '