春节闲赋在家无事,把博客又重新布置了一遍。分别把四个分类目录”札记“,”兴趣“,“琐忆”,“电脑“,名字改为:”生活漫步“(shenghuo-manbu), “兴趣发现”(xingqu-faxian),“ 时光絮语”(shiguang-xuyu),“ 技术笔记”(/jishu-biji),重设别名。又新增加“碎语札记”(suiyu-zhaji)版块。
没流量就不在意流量,彻底的改一遍,重置不是整改开天窗,是扒房子重建。索性把过去复杂各种伪静态,改成简短有未来感的”/%postname%/“结构。并优化关键词和描述。
<?php if (is_front_page()) : ?>
<meta name="keywords" content="载润札记, 个人博客, 生活感悟, 旅行摄影, 技术分享, 风俗文化, 生活点滴, 碎语札记, 兴趣探索" />
<meta name="description" content="载润札记,我的个人博客,日常碎语札记和你一同感受生活感悟,分享旅行摄影的乐趣,探索技术分享的奥秘,领略风俗文化的风情,记录生活点滴和兴趣探索。期待在这里与你相遇!!" />
<link rel="canonical" href="<?php echo esc_url(home_url('/')); ?>" />
<?php endif; ?>
添加functions.php文件完整代码
// 注册碎语文章类型
function register_suiyu_post_type() {
$args = 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' => '回收站中没有碎语',
'menu_name' => '碎语',
),
'public' => true,
'has_archive' => false,
'show_in_rest' => true,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
'rewrite' => array('slug' => 'suiyu-item', 'with_front' => false),
'menu_position' => 5,
'show_ui' => true,
'show_in_menu' => true,
'taxonomies' => array('category', 'post_tag'),
'menu_icon' => 'dashicons-format-status',
);
register_post_type('suiyu', $args);
}
add_action('init', 'register_suiyu_post_type');
// 自动修改标题
function add_prefix_to_suiyu_title($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
if ('suiyu' !== get_post_type($post_id)) return $post_id;
$post_title = get_post_field('post_title', $post_id);
if (empty($post_title)) {
$date = get_the_date('Y-m-d', $post_id);
$excerpt = wp_trim_words(strip_tags(get_the_content(null, false, $post_id)), 5, '...');
$new_title = "碎语 | $date - $excerpt";
} elseif (strpos($post_title, '碎语 | ') !== 0) {
$new_title = '碎语 | ' . $post_title;
} else {
return $post_id;
}
remove_action('save_post', 'add_prefix_to_suiyu_title');
wp_update_post(array('ID' => $post_id, 'post_title' => $new_title));
add_action('save_post', 'add_prefix_to_suiyu_title');
error_log('Updated suiyu title for post ID ' . $post_id . ' to: ' . $new_title);
return $post_id;
}
add_action('save_post', 'add_prefix_to_suiyu_title', 10, 1);
// 主题设置
function theme_setup() {
add_theme_support('comments');
add_theme_support('post-thumbnails');
add_post_type_support('post', 'comments');
add_post_type_support('suiyu', 'comments');
}
add_action('after_setup_theme', 'theme_setup');
// 加载脚本和样式
function enqueue_scripts_and_styles() {
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css', array(), '5.15.4');
wp_enqueue_script('jquery', 'https://code.jquery.com/jquery-3.6.0.min.js', array(), '3.6.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_scripts_and_styles');
// 评论处理
function preprocess_comment_handler($commentdata) {
if (!empty($_POST['honeypot'])) wp_die('检测到异常提交,请勿重复尝试!');
if (!isset($_POST['comment_nonce']) || !wp_verify_nonce($_POST['comment_nonce'], 'comment_nonce')) wp_die('验证失败,请重试!');
$commentdata['comment_approved'] = 0;
error_log('Comment processed for post ID ' . $commentdata['comment_post_ID']);
return $commentdata;
}
add_filter('preprocess_comment', 'preprocess_comment_handler');
add_action('comment_form', function() {
echo '<input type="hidden" name="comment_nonce" value="' . wp_create_nonce('comment_nonce') . '">';
});
// 关键词、描述和结构化数据
add_action('wp_head', function() {
$keywords = '载润札记, 个人博客, 生活感悟, 旅行摄影, 技术分享, 风俗文化, 生活点滴, 碎语札记, 兴趣探索';
$description = '载润札记,我的个人博客,日常碎语札记和你一同感受生活感悟,分享旅行摄影的乐趣,探索技术分享的奥秘,领略风俗文化的风情,记录生活点滴和兴趣探索。期待在这里与你相遇!';
if (is_page_template('page-suiyu.php') || (is_archive() && 'suiyu' === get_post_type())) {
$page_number = get_query_var('paged') ? get_query_var('paged') : 1;
$keywords = '碎语札记第' . $page_number . '页, 生活摄影, 日常感悟, 碎片化记录, 灵感分享, 生活点滴第' . $page_number . '页, 摄影随笔';
$description = '碎语札记第' . $page_number . '页,浏览更多生活摄影作品与日常感悟,碎片化记录生活的点滴灵感。';
$canonical_url = $page_number > 1 ? esc_url(home_url('/suiyu-zhaji/page/' . $page_number . '/')) : esc_url(home_url('/suiyu-zhaji/'));
echo '<link rel="canonical" href="' . $canonical_url . '" />';
} elseif (is_singular('suiyu')) {
$post_id = get_the_ID();
$keywords = '碎语札记, ' . esc_html(get_the_title()) . ', 生活摄影, 日常感悟, 碎片化记录, 灵感分享';
$description = '《' . esc_html(get_the_title()) . '》 - 碎语札记中的生活摄影与日常感悟,碎片化记录灵感与点滴。';
echo '<link rel="canonical" href="' . esc_url(get_permalink()) . '" />';
// 添加结构化数据
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'BlogPosting',
'headline' => get_the_title(),
'datePublished' => get_the_date('c'),
'author' => array(
'@type' => 'Person',
'name' => get_the_author()
),
'url' => get_permalink(),
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => get_permalink()
)
);
echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>';
}
echo '<meta name="keywords" content="' . esc_attr($keywords) . '">' . PHP_EOL;
echo '<meta name="description" content="' . esc_attr($description) . '">' . PHP_EOL;
});
?>
主题自定义标签美化代码
/* 页脚整体样式 */
footer {
text-align: center; /* 所有内容居中 */
}
/* 版权信息 */
footer .copyright {
font-size: 16px; /* 适中的字体大小 */
margin-bottom: 10px; /* 内容之间的间距 */
color: #003264; /* 字体颜色 */
}
/* 页脚链接 */
footer .footer-links {
font-size: 14px; /* 更小的字体 */
color: #3C82DC; /* 链接颜色 */
}
footer .footer-links a {
color: #3C82DC; /* 链接颜色 */
text-decoration: none; /* 去掉下划线 */
margin: 0 15px; /* 链接间距 */
}
footer .footer-links a:hover {
text-decoration: underline; /* 悬停时显示下划线 */
}
/* 移动端适配 */
@media (max-width: 768px) {
footer {
padding: 10px; /* 减少内边距 */
}
footer .copyright {
font-size: 14px; /* 调整字体大小 */
}
footer .footer-links {
font-size: 14px; /* 更小的字体 */
}
}
a {
color: #3C82DC;
}
a:hover {
color: #005EBB;
}
/* 美化标签方框 */
.tagcloud {
display: flex;
flex-wrap: wrap;
gap: 12px; /* 标签之间适度间距 */
}
.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: #0066CC !important; /* 悬停时浅金色背景 */
color: #fff !important; /* 悬停时文字颜色变为白色 */
transform: scale(1.05) !important; /* 放大效果更温和 */
}
文章照片灯箱特效,添加模板functions.php文件代码
// 添加灯箱功能到文章图片
function add_image_lightbox() {
if (is_single() && 'post' == get_post_type()) { // 仅文章页生效
?>
<style>
/* 灯箱容器 */
.lightbox-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.9);
display: none;
z-index: 9999;
cursor: zoom-out;
}
/* 放大后的图片 */
.lightbox-image {
position: absolute;
max-width: 90%;
max-height: 90%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: transform 0.3s ease;
cursor: grab;
}
/* 图片加载指示器 */
.lightbox-loading {
width: 50px;
height: 50px;
border: 3px solid #fff;
border-radius: 50%;
border-top-color: transparent;
animation: spin 1s linear infinite;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
@keyframes spin {
to { transform: translate(-50%, -50%) rotate(360deg); }
}
/* 文章图片点击样式 */
.entry-content img {
cursor: zoom-in;
transition: transform 0.2s;
}
.entry-content img:hover {
transform: scale(1.02);
}
</style>
<div class="lightbox-overlay">
<div class="lightbox-loading"></div>
<img class="lightbox-image" src="" alt="Enlarged view">
</div>
<script>
(function() {
// 初始化灯箱
const overlay = document.querySelector('.lightbox-overlay');
const lightboxImg = overlay.querySelector('img');
let isDragging = false;
let startX, startY, translateX = 0, translateY = 0;
// 点击图片触发
document.querySelectorAll('.entry-content img').forEach(img => {
if (!img.closest('a')) { // 排除已有链接的图片
img.style.cursor = 'zoom-in';
img.addEventListener('click', function(e) {
e.preventDefault();
showLightbox(this.src, this.alt);
});
}
});
// 显示灯箱
function showLightbox(src, alt) {
overlay.style.display = 'block';
lightboxImg.style.transform = 'translate(-50%, -50%) scale(0.8)';
lightboxImg.style.opacity = '0';
// 预加载高清图(如果有data-large属性)
const largeSrc = src.replace(/-\d+x\d+\./, '.'); // 自动去除WordPress缩略图尺寸
const imgLoader = new Image();
imgLoader.onload = () => {
lightboxImg.src = largeSrc;
lightboxImg.alt = alt;
lightboxImg.style.transform = 'translate(-50%, -50%) scale(1)';
lightboxImg.style.opacity = '1';
overlay.querySelector('.lightbox-loading').style.display = 'none';
};
imgLoader.src = largeSrc;
}
// 关闭灯箱
overlay.addEventListener('click', (e) => {
if (e.target === overlay || e.target === lightboxImg) {
overlay.style.display = 'none';
lightboxImg.src = '';
translateX = translateY = 0; // 重置位置
}
});
// 移动端手势支持
let touchStartX, touchStartY;
lightboxImg.addEventListener('touchstart', (e) => {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
}, { passive: true });
lightboxImg.addEventListener('touchmove', (e) => {
e.preventDefault(); // 阻止页面滚动
const touch = e.touches[0];
const deltaX = touch.clientX - touchStartX;
const deltaY = touch.clientY - touchStartY;
lightboxImg.style.transform = `translate(calc(-50% + ${deltaX}px), calc(-50% + ${deltaY}px))`;
}, { passive: false });
lightboxImg.addEventListener('touchend', () => {
touchStartX = touchStartY = null;
lightboxImg.style.transform = 'translate(-50%, -50%)';
});
// 键盘控制
document.addEventListener('keydown', (e) => {
if (overlay.style.display === 'block') {
if (e.key === 'Escape') overlay.style.display = 'none';
if (e.key === 'ArrowLeft') navigate(-1);
if (e.key === 'ArrowRight') navigate(1);
}
});
})();
</script>
<?php
}
}
add_action('wp_footer', 'add_image_lightbox');
分页
/* 上一页/下一页按钮样式 */
.nav-links .prev,
.nav-links .next {
background-color: #0066CC; /* WordPress 默认蓝色 */
color: white !important;
}
.nav-links .prev:hover,
.nav-links .next:hover {
background-color: #005EBB; /* 悬停颜色 */
}
/* 当前页码样式 */
.nav-links .current {
background-color: #ddd;
color: #222;
}
/* 链接悬停效果 */
.nav-links a:hover {
background-color: #e0e0e0;
color: #000;
}
添加百度必应地图推送日志
// 在 submit_sitemap_to_baidu 中
if ($status == 200 && strpos($body, '"success"') !== false) {
error_log('百度推送成功: ' . date('Y-m-d H:i:s')); // 临时添加
update_option('psp_last_push_status', '百度推送成功: ' . date('Y-m-d H:i:s'));
}
// 在 submit_sitemap_to_bing_indexnow 中
if ($status == 200) {
error_log('必应推送成功: ' . date('Y-m-d H:i:s')); // 临时添加
update_option('psp_last_push_status', '必应推送成功: ' . date('Y-m-d H:i:s'));
}
本文由载润札记原创发布,转载请注明出处:博客重置分类固定链接笔札。
扫描二维码阅读文章