空笺未寄 v1.0 — 基于 DeepSeek UI 的 WordPress 主题
页面:首页、笺文、微言、拾影、文章详情、作者页 功能:三种展示方式、tucao 自定义类型、评论、分页、灯箱 CSS:DeepSeek 原 CSS + 系统字体 + 头像本地化 安装:激活主题后自动创建页面和导航,刷新固定链接即可
This commit is contained in:
+190
@@ -0,0 +1,190 @@
|
||||
<?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;
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 禁用 Gravatar — 本地默认头像
|
||||
add_filter('get_avatar', function($avatar, $id_or_email, $size, $default, $alt) {
|
||||
// 用 data URI 的默认灰色圆圈替代
|
||||
$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="' . $size . '" height="' . $size . '"><circle cx="20" cy="20" r="20" fill="#e0d6c5"/><circle cx="20" cy="15" r="7" fill="#c4826e"/><ellipse cx="20" cy="32" rx="13" ry="9" fill="#c4826e"/></svg>';
|
||||
return '<span class="avatar-local" style="display:inline-flex;align-items:center;vertical-align:middle;width:' . $size . 'px;height:' . $size . 'px;border-radius:50%;overflow:hidden;">' . $svg . '</span>';
|
||||
}, 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();
|
||||
}
|
||||
Reference in New Issue
Block a user