Files
kjweiji/functions.php
T
liaoxin 16c3e10494 v1.1 — 图片/头像/作者/日期全面优化
- 图片: 缩略图改为 soft crop,原图裁黑边
- 头像: 团队证件照替换 SVG,CSS 约束尺寸和圆角
- 作者: 了心/墨羽/探子三个真实用户
- 笺文列表: 左侧增加封面缩略图
- 分页: 20篇/页
- 详情页: 返回笺文链接 + 头像尺寸约束
- 作者页: 高清头像 + 中文日期
- 微言: 作者随机 + 时间匹配内容
2026-05-03 23:59:38 +08:00

199 lines
8.0 KiB
PHP

<?php
/**
* 空笺未寄 — functions.php
*/
// ── 主题设置 ──────────────────────────────────
add_action('after_setup_theme', 'kjweji_setup');
function kjweji_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_image_size('kjweji-featured', 960, 400, true);
add_image_size('kjweji-card', 600, 375, true);
add_theme_support('custom-logo', array(
'height' => 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 '<meta property="og:type" content="' . (is_singular() ? 'article' : 'website') . '" />' . "\n";
echo '<meta property="og:title" content="' . esc_attr($title) . '" />' . "\n";
echo '<meta property="og:description" content="' . esc_attr($desc) . '" />' . "\n";
echo '<meta property="og:url" content="' . esc_url($url) . '" />' . "\n";
if (is_singular() && has_post_thumbnail()) {
$img = get_the_post_thumbnail_url(null, 'kjweji-featured');
echo '<meta property="og:image" content="' . esc_url($img) . '" />' . "\n";
}
echo '<meta name="twitter:card" content="summary_large_image" />' . "\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'] = '<p class="comment-form-author"><label for="author">显示名称</label><input id="author" name="author" type="text" size="30" /></p>';
$fields['email'] = '<p class="comment-form-email"><label for="email">邮箱</label><input id="email" name="email" type="email" size="30" /></p>';
$fields['url'] = '<p class="comment-form-url"><label for="url">网站</label><input id="url" name="url" type="url" size="30" /></p>';
$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes" /><label for="wp-comment-cookies-consent">在此浏览器中保存我的信息</label></p>';
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 '<img src="' . esc_url($url) . '" class="avatar avatar-' . $size . ' photo" width="' . $size . '" height="' . $size . '" alt="" />';
}
}
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();
}