Files
gongxue-wp-theme/functions.php
2026-06-25 15:57:22 +08:00

280 lines
13 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
if (!defined('ABSPATH')) {
exit;
}
function gx_theme_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script'));
add_theme_support('custom-logo', array('height' => 80, 'width' => 80, 'flex-height' => true, 'flex-width' => true));
add_theme_support('align-wide');
add_theme_support('responsive-embeds');
register_nav_menus(array(
'primary' => __('主导航菜单', 'gongxue'),
));
}
add_action('after_setup_theme', 'gx_theme_setup');
function gx_enqueue_assets() {
$theme_dir = get_template_directory();
$theme_uri = get_template_directory_uri();
wp_enqueue_script('tailwindcss', 'https://cdn.tailwindcss.com', array(), '3.4', false);
wp_add_inline_script('tailwindcss', 'tailwind.config={theme:{extend:{colors:{"apple-grey":"#F5F5F7","apple-dark":"#1D1D1F","apple-blue":"#0071E3","apple-blue-hover":"#0077ED","apple-text-gray":"#86868B"},animation:{"fade-in":"fadeIn .6s ease-out forwards","slide-up":"slideUp .6s ease-out forwards","slide-down":"slideDown .4s ease-out forwards"},keyframes:{fadeIn:{"0%":{opacity:"0"},"100%":{opacity:"1"}},slideUp:{"0%":{opacity:"0",transform:"translateY(20px)"},"100%":{opacity:"1",transform:"translateY(0)"}},slideDown:{"0%":{opacity:"0",transform:"translateY(-10px)"},"100%":{opacity:"1",transform:"translateY(0)"}}}}}};');
wp_enqueue_style('gx-style', $theme_uri . '/assets/css/main.css', array(), filemtime($theme_dir . '/assets/css/main.css'));
wp_enqueue_style('gx-fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap', array(), null);
wp_enqueue_script('gx-main', $theme_uri . '/assets/js/main.js', array(), filemtime($theme_dir . '/assets/js/main.js'), true);
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
}
add_action('wp_enqueue_scripts', 'gx_enqueue_assets');
function gx_knowledge_url() {
$page_id = get_option('page_for_posts');
return $page_id ? get_permalink($page_id) : home_url('/knowledge/');
}
function gx_linked_page_item($page, $fallback_label, $active = false) {
if (!$page || is_wp_error($page)) {
return array(
'label' => $fallback_label,
'url' => home_url('/'),
'active' => $active,
);
}
return array(
'label' => get_the_title($page),
'url' => get_permalink($page),
'active' => $active,
);
}
function gx_primary_nav_items() {
$front_page = get_post((int) get_option('page_on_front'));
$knowledge_page = get_post((int) get_option('page_for_posts'));
$question_bank = get_page_by_path('question-bank', OBJECT, 'page');
$online_school = get_page_by_path('online-school', OBJECT, 'page');
$books_page = get_page_by_path('books', OBJECT, 'page');
return array_filter(array(
gx_linked_page_item($front_page, __('首页', 'gongxue'), is_front_page()),
gx_linked_page_item($question_bank, __('刷题题库', 'gongxue'), is_page('question-bank')),
gx_linked_page_item($online_school, __('在线网校', 'gongxue'), is_page('online-school')),
gx_linked_page_item($books_page, __('出版书籍', 'gongxue'), is_page('books')),
gx_linked_page_item($knowledge_page, __('知识库', 'gongxue'), is_home() || is_archive() || is_single()),
));
}
function gx_format_views($count) {
$count = intval($count);
if ($count >= 10000) return round($count / 10000, 1) . 'w+';
if ($count >= 1000) return round($count / 1000, 1) . 'k';
return (string) $count;
}
function gx_get_views($post_id = null) {
$post_id = $post_id ?: get_the_ID();
return intval(get_post_meta($post_id, '_gx_view_count', true));
}
function gx_inc_views() {
if (is_admin() || wp_doing_ajax() || wp_doing_cron() || is_feed() || !is_singular('post')) {
return;
}
$id = get_queried_object_id();
if (!$id) return;
$count = gx_get_views($id) + 1;
update_post_meta($id, '_gx_view_count', $count);
}
add_action('template_redirect', 'gx_inc_views');
function gx_pagination() {
global $wp_query;
$links = paginate_links(array(
'total' => $wp_query->max_num_pages,
'current' => max(1, get_query_var('paged')),
'type' => 'array',
'prev_text' => '',
'next_text' => '',
));
if (!$links) return;
echo '<div class="flex justify-center mt-12"><nav class="flex items-center gap-2">';
foreach ($links as $link) {
$active = strpos($link, 'current') !== false;
echo '<span class="' . ($active ? 'px-4 py-2 bg-apple-blue text-white rounded-full text-sm' : 'px-4 py-2 text-gray-500 hover:text-apple-dark hover:bg-gray-100 rounded-full text-sm') . '">' . $link . '</span>';
}
echo '</nav></div>';
}
function gx_excerpt_len($len) { return 30; }
add_filter('excerpt_length', 'gx_excerpt_len');
function gx_excerpt_more($more) { return '...'; }
add_filter('excerpt_more', 'gx_excerpt_more');
function gx_register_sidebars() {
register_sidebar(array(
'name' => __('知识库侧边栏', 'gongxue'),
'id' => 'knowledge-sidebar',
'before_widget' => '<div class="bg-white p-6 rounded-2xl shadow-sm mb-4">',
'after_widget' => '</div>',
'before_title' => '<h3 class="text-lg font-bold text-apple-dark mb-4">',
'after_title' => '</h3>',
));
}
add_action('widgets_init', 'gx_register_sidebars');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_shortlink_wp_head');
function gx_allow_svg($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'gx_allow_svg');
function gx_seo_description() {
if (is_singular()) {
$excerpt = trim(wp_strip_all_tags(get_the_excerpt()));
if ($excerpt) {
return $excerpt;
}
$content = wp_strip_all_tags(get_the_content());
return wp_trim_words($content, 28, '');
}
if (is_home()) {
$page_id = get_option('page_for_posts');
if ($page_id) {
$excerpt = trim(wp_strip_all_tags(get_post_field('post_excerpt', $page_id)));
if ($excerpt) return $excerpt;
}
return get_bloginfo('description');
}
if (is_front_page()) {
return '赋能未来,让升本更简单。恭学教育提供天津专升本刷题题库、在线网校、出版书籍与知识库服务。';
}
if (is_category() || is_tag()) {
$term = get_queried_object();
if ($term && !is_wp_error($term) && !empty($term->description)) {
return wp_strip_all_tags($term->description);
}
}
return get_bloginfo('description');
}
function gx_seo_title() {
if (is_front_page()) {
return '恭学教育 - 天津专升本培训领军品牌 | 刷题题库 | 在线网校 | 出版书籍';
}
if (is_home()) {
return '知识库 - 天津专升本百科全书';
}
if (is_category()) {
$term = get_queried_object();
return ($term && !is_wp_error($term)) ? $term->name . ' - 知识库' : '知识库';
}
if (is_tag()) {
$term = get_queried_object();
return ($term && !is_wp_error($term)) ? '#' . $term->name . ' - 知识库' : '知识库';
}
if (is_singular('post')) {
return get_the_title() . ' - 知识库';
}
if (is_page()) {
return get_the_title();
}
return wp_get_document_title();
}
function gx_seo_image() {
if (is_singular() && has_post_thumbnail()) {
$image = get_the_post_thumbnail_url(null, 'full');
if ($image) return $image;
}
$cfg = gx_asset_config();
return $cfg['hero_video_cover'];
}
function gx_output_seo_meta() {
if (is_admin()) return;
$title = gx_seo_title();
$desc = gx_seo_description();
$image = gx_seo_image();
$url = is_singular() ? get_permalink() : (is_home() ? gx_knowledge_url() : (is_front_page() ? home_url('/') : home_url(add_query_arg(array(), $GLOBALS['wp']->request ?? ''))));
echo '<meta name="description" content="' . esc_attr($desc) . '">' . "\n";
echo '<meta name="keywords" content="' . esc_attr(get_bloginfo('name') . ',天津专升本,恭学教育,刷题题库,在线网校,出版书籍,知识库') . '">' . "\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";
echo '<meta property="og:type" content="' . esc_attr(is_singular() ? 'article' : 'website') . '">' . "\n";
echo '<meta property="og:image" content="' . esc_url($image) . '">' . "\n";
echo '<meta name="twitter:card" content="summary_large_image">' . "\n";
echo '<meta name="twitter:title" content="' . esc_attr($title) . '">' . "\n";
echo '<meta name="twitter:description" content="' . esc_attr($desc) . '">' . "\n";
echo '<meta name="twitter:image" content="' . esc_url($image) . '">' . "\n";
echo '<link rel="canonical" href="' . esc_url($url) . '">' . "\n";
if (is_singular('post')) {
$author = get_the_author();
$date = get_the_date('c');
$headline = get_the_title();
$article = array(
'@context' => 'https://schema.org',
'@type' => 'Article',
'headline' => $headline,
'description' => $desc,
'author' => array('@type' => 'Person', 'name' => $author),
'datePublished' => $date,
'dateModified' => get_the_modified_date('c'),
'mainEntityOfPage' => $url,
'image' => array($image),
'publisher' => array('@type' => 'Organization', 'name' => get_bloginfo('name')),
);
echo '<script type="application/ld+json">' . wp_json_encode($article, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
}
}
add_action('wp_head', 'gx_output_seo_meta', 1);
function gx_asset_config() {
return array(
'hero_video_url' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E6%81%AD%E5%AD%A6%E6%95%99%E8%82%B2%E5%93%81%E7%89%8C%E5%AE%A3%E4%BC%A0.mp4',
'hero_video_cover' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E5%B0%81%E9%9D%A2.png',
'question_bank_mockup' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/tikudemo-tuya.webp',
'online_school_mockup' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/wangkedemo-tuya.webp',
'customer_service_qrcode' => get_template_directory_uri() . '/assets/images/customer-service-qrcode.jpg',
'stats' => array('years' => 7, 'students' => '21W+', 'coverage' => '100%', 'schools' => 12),
'books' => array(
array('title' => '天津专升本数学自学参考书', 'price' => '128.00', 'old' => '168.00', 'image' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E6%95%B0%E5%AD%A61-tuya.webp', 'tag' => '畅销榜第一'),
array('title' => '天津专升本语文自学参考书', 'price' => '128.00', 'old' => '168.00', 'image' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E8%AF%AD%E6%96%871-tuya.webp', 'tag' => '新书首发'),
array('title' => '天津专升本英语自学参考书', 'price' => '128.00', 'old' => '168.00', 'image' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E8%8B%B1%E8%AF%AD1-tuya.webp', 'tag' => '热销'),
array('title' => '天津专升本计算机自学参考书', 'price' => '128.00', 'old' => '168.00', 'image' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%80%92%E5%BD%B1-tuya.webp', 'tag' => '必刷'),
array('title' => '三年模拟一年升本刷题系列套装', 'price' => '128.00', 'old' => '198.00', 'image' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E4%B8%89%E4%B8%80-tuya.webp', 'tag' => '限时优惠'),
),
'team' => array(
array('name' => '陈奕静老师', 'role' => '七年专业课教龄王牌老师', 'avatar' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E9%99%88%E5%A5%95%E9%9D%99-tuya.webp'),
array('name' => '郭馨老师', 'role' => '七年专业课教龄王牌老师', 'avatar' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E9%83%AD%E9%A6%A8%20%282%29-%E8%AE%A1%E7%AE%97%E6%9C%BA-tuya.webp'),
array('name' => '师哲老师', 'role' => '教学总监', 'avatar' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E5%B8%88%E5%93%B22-tuya.webp'),
array('name' => '王沐妍老师', 'role' => '七年专业课教龄王牌老师', 'avatar' => 'https://fenxiang-1304851894.cos.ap-nanjing.myqcloud.com/%E7%8E%8B%E6%B2%90%E5%A6%8D-tuya.webp'),
),
);
}
function gx_phone_mockup($image, $class = '') {
?>
<div class="gx-phone <?php echo esc_attr($class); ?>">
<div class="gx-phone-frame">
<div class="gx-phone-screen">
<div class="gx-phone-island"></div>
<img src="<?php echo esc_url($image); ?>" alt="App Screen" loading="lazy">
<div class="gx-phone-home"></div>
</div>
</div>
</div>
<?php
}