子目录functions.php文件添加代码
<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/
/**
* GeneratePress Child Theme Functions
* 碎语札记相关功能
*/
// ===== 禁用内容自动转义,修复破折号等字符乱码 =====
remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
// 修复标题乱码
function fix_document_title_entities($title) {
if (isset($title)) {
$title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
}
return $title;
}
add_filter('document_title', 'fix_document_title_entities');
/**
* ===== 碎语轻量修复包 =====
* 只修复必要的问题,不破坏正常结构
*/
// 1. 修复MORE标签锚点(这是必要的)
add_filter('the_content_more_link', 'fix_micropost_more_link');
function fix_micropost_more_link($link) {
if (get_post_type() == 'micropost') {
$link = str_replace('#more-', '#post-', $link);
}
return $link;
}
// 2. 确保MORE标签在列表页正确截断
add_action('pre_get_posts', 'fix_micropost_archive_display');
function fix_micropost_archive_display($query) {
if (!is_admin() && $query->is_main_query() && is_post_type_archive('micropost')) {
add_filter('the_content', function($content) {
global $more;
$more = 0;
return $content;
}, 5);
}
}
// 3. 只过滤明显的错误标签,不破坏正常标签云
add_filter('get_the_terms', 'filter_error_micropost_tags', 10, 3);
function filter_error_micropost_tags($terms, $post_id, $taxonomy) {
// 只处理碎语标签
if ($taxonomy != 'micropost_tag' || empty($terms) || !is_array($terms)) {
return $terms;
}
foreach ($terms as $key => $term) {
if (is_object($term) && isset($term->name)) {
// 只删除 more- 开头的错误标签
if (strpos($term->name, 'more-') !== false) {
unset($terms[$key]);
}
// 注意:不要删除颜色代码标签,让它们正常显示
}
}
return $terms;
}
// ===== 自定义碎语 Feed =====
if ( ! function_exists( 'zr_register_suiyu_feed' ) ) {
function zr_register_suiyu_feed() {
add_feed( 'suiyu-zhaji-feed', 'zr_suiyu_feed_template' );
}
add_action( 'init', 'zr_register_suiyu_feed' );
}
if ( ! function_exists( 'zr_suiyu_feed_template' ) ) {
function zr_suiyu_feed_template() {
while ( ob_get_level() ) ob_end_clean();
add_action( 'pre_get_posts', 'zr_filter_suiyu_feed_query' );
load_template( ABSPATH . WPINC . '/feed-rss2.php' );
}
}
if ( ! function_exists( 'zr_filter_suiyu_feed_query' ) ) {
function zr_filter_suiyu_feed_query( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_feed( 'suiyu-zhaji-feed' ) ) {
$query->set( 'post_type', 'micropost' );
$query->set( 'posts_per_page', 15 );
$query->set( 'no_found_rows', true );
add_filter( 'pre_option_rss_use_excerpt', '__return_zero' );
}
}
}
// ===== 主 Feed 排除碎语文章 =====
if ( ! function_exists( 'zr_exclude_suiyu_from_main_feed' ) ) {
function zr_exclude_suiyu_from_main_feed( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_feed() && ! $query->is_feed( 'suiyu-zhaji-feed' ) ) {
$types = $query->get( 'post_type' );
if ( empty( $types ) ) $types = array( 'post' );
if ( ! is_array( $types ) ) $types = array( $types );
$types = array_diff( $types, array( 'micropost' ) );
$query->set( 'post_type', $types );
}
}
add_action( 'pre_get_posts', 'zr_exclude_suiyu_from_main_feed' );
}
// ===== 碎语Feed链接显示 =====
add_filter( 'feed_links_show_posts_feed', '__return_true' );
add_action( 'wp_head', 'zr_add_suiyu_feed_link' );
function zr_add_suiyu_feed_link() {
echo '<link rel="alternate" type="application/rss+xml" title="' . get_bloginfo('name') . ' » 碎语札记 Feed" href="' . get_feed_link( 'suiyu-zhaji-feed' ) . '" />' . "\n";
}
// ===== 修复小工具查询 =====
add_filter('widget_posts_args', 'zr_include_suiyu_in_widget');
function zr_include_suiyu_in_widget($args) {
$args['post_type'] = array('post', 'micropost');
return $args;
}
// ===== 最新碎语小工具 =====
add_action('widgets_init', 'zr_register_suiyu_widget');
function zr_register_suiyu_widget() {
register_widget('ZR_Suiyu_Recent_Widget');
}
class ZR_Suiyu_Recent_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'zr_suiyu_recent',
'最新碎语',
array('description' => '仅显示碎语文章')
);
}
public function widget($args, $instance) {
$title = !empty($instance['title']) ? $instance['title'] : '最新碎语';
$number = !empty($instance['number']) ? $instance['number'] : 5;
$recent = new WP_Query(array(
'post_type' => 'micropost',
'posts_per_page' => $number,
'post_status' => 'publish',
'no_found_rows' => true
));
if ($recent->have_posts()) {
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
echo '<ul class="suiyu-widget-list">';
while ($recent->have_posts()) {
$recent->the_post();
echo '<li>';
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
echo '<span class="post-date">' . get_the_date('m-d') . '</span>';
echo '</li>';
}
echo '</ul>';
echo $args['after_widget'];
wp_reset_postdata();
}
}
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] : '最新碎语';
$number = !empty($instance['number']) ? $instance['number'] : 5;
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">标题:</label>
<input type="text" id="<?php echo $this->get_field_id('title'); ?>"
name="<?php echo $this->get_field_name('title'); ?>"
value="<?php echo esc_attr($title); ?>" class="widefat">
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>">显示数量:</label>
<input type="number" id="<?php echo $this->get_field_id('number'); ?>"
name="<?php echo $this->get_field_name('number'); ?>"
value="<?php echo esc_attr($number); ?>" min="1" max="20">
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['number'] = intval($new_instance['number']);
return $instance;
}
}
// ===== 动态去重碎语Feed =====
add_filter('posts_results', 'syzj_deduplicate_feed_posts', 10, 2);
function syzj_deduplicate_feed_posts($posts, $query) {
if ($query->is_feed('suiyu-zhaji-feed') && !is_admin()) {
$unique = array();
$titles = array();
foreach ($posts as $post) {
if (!in_array($post->post_title, $titles)) {
$unique[] = $post;
$titles[] = $post->post_title;
}
}
return $unique;
}
return $posts;
}
// ===== 在碎语单页禁用 Post Footer Info 插件(修复版) =====
add_action( 'wp', 'suiyu_disable_post_footer_info' );
function suiyu_disable_post_footer_info() {
if ( is_singular( 'micropost' ) ) {
add_filter( 'the_content', 'suiyu_remove_footer_info', 999 );
}
}
function suiyu_remove_footer_info( $content ) {
if ( is_singular( 'micropost' ) ) {
$content = preg_replace('/<div[^>]*class="[^"]*post-footer-info[^"]*"[^>]*>.*?<\/div>/is', '', $content);
$content = preg_replace('/<div[^>]*class="[^"]*related-posts[^"]*"[^>]*>.*?<\/div>/is', '', $content);
$content = preg_replace('/<section[^>]*class="[^"]*related-posts[^"]*"[^>]*>.*?<\/section>/is', '', $content);
$content = preg_replace('/<div[^>]*class="[^"]*copyright[^"]*"[^>]*>.*?<\/div>/is', '', $content);
}
return $content;
}
// ===== JavaScript 修复小工具图片 =====
add_action('wp_footer', 'suiyu_limit_widget_images');
function suiyu_limit_widget_images() {
if (wp_is_mobile()) {
?>
<script>
function limitWidgetImages() {
var images = document.querySelectorAll('.widget img, .suiyu-widget-list img');
for (var i = 0; i < images.length; i++) {
var img = images[i];
if (img.parentElement) {
img.style.maxWidth = '100%';
img.style.height = 'auto';
}
}
}
limitWidgetImages();
setTimeout(limitWidgetImages, 500);
window.addEventListener('load', limitWidgetImages);
window.addEventListener('resize', limitWidgetImages);
</script>
<?php
}
}
// ================================
// 碎语视频短代码(电脑竖屏自动变窄版)
// ================================
function suiyu_video_shortcode($atts) {
$atts = shortcode_atts(array(
'src' => '',
'title' => '',
), $atts, 'suiyu_video');
if (empty($atts['src'])) return '';
ob_start();
?>
<div class="suiyu-video-wrapper">
<video src="<?php echo esc_url($atts['src']); ?>"
controls
controlsList="nodownload"
preload="metadata"
loading="lazy"
class="suiyu-video">
</video>
<?php if (!empty($atts['title'])): ?>
<p class="suiyu-video-title">
<?php echo esc_html($atts['title']); ?>
</p>
<?php endif; ?>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
const videos = document.querySelectorAll(".suiyu-video");
videos.forEach(video => {
video.addEventListener("loadedmetadata", function() {
const wrapper = video.closest(".suiyu-video-wrapper");
const isPortrait = video.videoHeight > video.videoWidth;
if (window.innerWidth > 1024) {
// 电脑端
if (isPortrait) {
wrapper.style.maxWidth = "450px"; // 竖屏变窄
} else {
wrapper.style.maxWidth = "800px"; // 横屏
}
} else {
// 手机端
wrapper.style.maxWidth = "100%";
}
});
});
});
</script>
<style>
.suiyu-video-wrapper{
margin:20px auto;
text-align:center;
}
.suiyu-video{
width:100%;
height:auto;
border-radius:6px;
display:block;
}
.suiyu-video-title{
color:#666;
font-size:14px;
margin-top:8px;
}
</style>
<?php
return ob_get_clean();
}
add_shortcode('suiyu_video', 'suiyu_video_shortcode');
// ================================
// TinyMCE 插入视频按钮
// ================================
function suiyu_add_video_button() {
add_filter('mce_external_plugins', 'suiyu_video_plugin');
add_filter('mce_buttons', 'suiyu_register_video_button');
}
add_action('admin_init', 'suiyu_add_video_button');
function suiyu_video_plugin($plugin_array) {
$plugin_array['suiyu_video_button'] = get_stylesheet_directory_uri() . '/js/suiyu-video-button.js';
return $plugin_array;
}
function suiyu_register_video_button($buttons) {
array_push($buttons, 'suiyu_video_button');
return $buttons;
}标签美化CSS代码
/* 美化标签方框 */
.tagcloud {
display: flex;
flex-wrap: wrap;
gap: 1px; /* 标签之间适度间距 */
}
.tagcloud a {
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #E5F1FB; /* 精致的浅金色边框 */
color: #3C82DC !important; /* 深灰色字体 */
padding: 6px 14px !important; /* 较小的内边距 */
margin: 0 !important;
border-radius: 2px !important; /* 圆角效果 */
font-size: 13px !important; /* 小巧的字体 */
text-decoration: none !important;
transition: all 0.3s ease;
min-width: 80px; /* 较小的最小宽度 */
height: 45px; /* 较小的高度 */
box-sizing: border-box;
}
.tagcloud a:hover {
background-color: #247BFF !important; /* 悬停时浅金色背景 */
color: #fff !important; /* 悬停时文字颜色变为白色 */
transform: scale(1.05) !important; /* 放大效果更温和 */
}
/* ===== 手机端终极修复 ===== */
@media only screen and (max-width: 768px) {
/* 评论头像强制缩小 */
.comment-author img.avatar,
img.avatar,
.avatar {
width: 32px !important;
height: 32px !important;
max-width: 32px !important;
min-width: 32px !important;
border-radius: 50% !important;
}
/* FEED 标识图片保持原大小 */
a[href*="/feed/"] img,
img[alt*="RSS"],
img[alt*="Feed"],
div[style*="text-align: center"] img {
width: auto !important;
max-width: none !important;
height: auto !important;
max-height: 40px !important; /* 限制最大高度 */
}
@media screen and (max-width: 768px) {
@media screen and (max-width: 768px) {
/* 强制覆盖主题自带的响应式图片规则 */
.header-site-branding img.attachment-full.size-full,
.site-logo img {
width: 60% !important; /* 调整为想要的大小 */
max-width: 300px !important;
height: auto !important;
}
}文件添加代码
<?php
/**
* The template for displaying the footer.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
?>
</div>
</div>
<?php
/**
* generate_before_footer hook.
*
* @since 0.1
*/
do_action( 'generate_before_footer' );
?>
<footer id="colophon" class="site-footer" role="contentinfo">
<div class="site-info">
<p class="copyright">风雨几十载 润物细无声 | 载润札记,致力于分享生活、
旅行与文化的点滴</p>
<p class="copyright">Copyright © 2006 载润札记 All Rights Reserved.</p>
<div class="footer-links">
<a href="https://www.zairun.com/guanyuwo/">关于我们</a> |
<a href="https://www.zairun.com/liuyanban/">联系我们</a> |
<a href="https://www.zairun.com/sitemap.html">网站地图</a>
</div>
</div>
</footer>
<?php
/**
* generate_after_footer hook.
*
* @since 2.1
*/
do_action( 'generate_after_footer' );
wp_footer();
?>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?d17a7e92fe4b0f7c1900c46936e9205a";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?36271ef1ddeebf3b27129eebbc50473e";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<!-- Clarity tracking code for http://www.zairun.com/ -->
<?php if (is_singular('micropost')): ?>
<!-- Prism.js 代码高亮 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script>
// 设置 Prism 自动加载路径
Prism.plugins.autoloader.languages_path = 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/';
</script>
<?php endif; ?>
</body>
</html>文章编辑器添加视频JS代码
/**
* 简单图片灯箱脚本
*/
(function($) {
'use strict';
// 只在页面加载完成后执行
$(document).ready(function() {
// 创建灯箱容器
if ($('#simple-lightbox-overlay').length === 0) {
$('body').append('<div id="simple-lightbox-overlay"><span class="close-btn">×</span><span class="counter"></span><img src="" alt=""></div>');
}
var $overlay = $('#simple-lightbox-overlay');
var $img = $overlay.find('img');
var $counter = $overlay.find('.counter');
var images = [];
var currentIndex = 0;
// 收集所有文章图片
$('.entry-content img, .post-content img, .suiyu-azure-content img, article img').each(function() {
var $this = $(this);
// 跳过小图标
if ($this.width() < 50 || $this.hasClass('avatar') || $this.hasClass('emoji')) {
return;
}
images.push({
src: $this.attr('src'),
element: this
});
// 添加点击事件
$this.css('cursor', 'pointer');
$this.on('click', function(e) {
e.preventDefault();
// 找到点击的图片索引
var src = $(this).attr('src');
for (var i = 0; i < images.length; i++) {
if (images[i].src === src) {
currentIndex = i;
break;
}
}
// 显示灯箱
$img.attr('src', src);
if (images.length > 1) {
$counter.text((currentIndex + 1) + ' / ' + images.length).show();
} else {
$counter.hide();
}
$overlay.addClass('active');
$('body').css('overflow', 'hidden');
});
});
// 关闭灯箱
$overlay.on('click', function(e) {
if (e.target === this || $(e.target).hasClass('close-btn')) {
$overlay.removeClass('active');
$('body').css('overflow', '');
}
});
// 键盘事件
$(document).on('keydown', function(e) {
if (!$overlay.hasClass('active')) return;
if (e.keyCode === 27) { // ESC
$overlay.removeClass('active');
$('body').css('overflow', '');
}
});
console.log('简单灯箱初始化完成,找到 ' + images.length + ' 张图片');
});
})(jQuery);