1、首先我们把以下代码,添加到主题根目录中的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' => true, // 是否支持归档页面
'show_in_rest' => true, // 启用Gutenberg编辑器
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt'), // 支持字段
'rewrite' => array(
'slug' => 'suiyu', // 文章别名,设置成 'suiyu',
'with_front' => false, // 禁止前缀(例如首页)
'feeds' => true, // 支持 RSS
'pages' => true, // 支持分页
),
'menu_position' => 5, // 菜单中的位置
'show_ui' => true, // 显示UI界面
'show_in_menu' => true, // 在后台菜单中显示
'taxonomies' => array('category', 'post_tag'), // 关联分类和标签
'has_archive' => true, // 是否启用归档
'menu_icon' => 'dashicons-format-status', // 菜单图标
);
register_post_type('suiyu', $args);
}
add_action('init', 'register_suiyu_post_type');
// 为「碎语」文章类型生成专属 RSS 源
add_action('init', 'suiyu_custom_rss_feed');
function suiyu_custom_rss_feed() {
add_feed('suiyu', 'suiyu_rss_template');
}
2、然后添加模板文件,在模板主题根目录新建一个名为page-suiyu.php的文件,并将以下代码添加在其中
<?php
/**
* Template Name: 碎语/说说
*/
get_header(); ?>
<?php
// 在wp_head中添加关键词和描述
add_action('wp_head', function() {
// 添加关键词
$keywords = '碎语札记, 生活, 记录, 日常感悟, 灵感, 碎片化, 每日灵感碎片';
echo '<meta name="keywords" content="' . esc_attr($keywords) . '">';
// 添加描述
$description = '日常碎语札记,记录生活中的点滴感悟,分享摄影作品和那些转瞬即逝的灵感。不拘泥于形式,不追求完整,只言片语,碎片化生活记录。';
echo '<meta name="description" content="' . esc_attr($description) . '">';
});
?>
<style>
/* 分页样式 */
.pagination {
text-align: center;
margin-top: 20px;
}
.pagination a, .pagination span {
padding: 8px 16px;
margin: 0 4px;
background-color: #24a0f0;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s ease;
}
.pagination a:hover, .pagination span:hover {
background-color: #7878f0;
}
/* 碎语列表项样式 */
.cbp_tmtimeline > li {
margin-bottom: 20px;
padding: 15px;
background-color: #FFFFF9;
border-left: 5px solid #E9E3C9;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
border-radius: 8px;
transition: all 0.3s ease;
display: flex;
align-items: center;
}
.cbp_tmtimeline > li:hover {
transform: translateY(-5px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
/* 去掉碎语内容的默认下划线 */
.cbp_tmtimeline .cbp_tmlabel {
text-decoration: none !important;
color: inherit !important;
display: flex;
flex-direction: column;
}
/* 确保 <p> 内文本不会受超链接影响 */
.cbp_tmtimeline .cbp_tmlabel p {
text-decoration: none !important;
}
/* 调整发布日期的间距 */
.suiyu_time {
font-size: 0.9em;
color: #777;
margin-top: 10px;
margin-bottom: 5px;
}
/* 头像样式 */
.suiyu_author_img {
margin-right: 15px;
}
.suiyu_author_img img {
border: 1px solid #ddd;
padding: 2px;
float: left;
border-radius: 64px;
transition: all 1.0s;
height: 50px;
}
.avatar {
border-radius: 100% !important;
}
.zhuan {
transform: rotateZ(720deg);
}
</style>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="cbp_suiyu">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'suiyu',
'post_status' => 'publish',
'posts_per_page' => 16,
'paged' => $paged,
);
$query = new WP_Query($args);
if ($query->have_posts()) : ?>
<ul class="cbp_tmtimeline">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<li>
<span class="suiyu_author_img">
<img src="<?php echo esc_url(get_avatar_url(get_the_author_meta('ID'))); ?>" class="avatar avatar-48" width="48" height="48">
</span>
<!-- 这里修改,把 a 改成 div,去掉 href -->
<div class="cbp_tmlabel">
<p><?php the_content(); ?></p>
<p class="suiyu_time">
<i class="fa fa-clock-o"></i> <?php echo get_the_time('Y年n月j日G:i'); ?><span class="page-views" style="margin-left: 10px; font-size: 14px; color: #666;">
<?php
if ( function_exists( 'pvc_get_post_views' ) ) {
$page_views = pvc_get_post_views( get_the_ID() );
echo '阅读: ' . $page_views;
}
?>
</span>
</p><?php
if (function_exists('wp_ulike')) {
echo do_shortcode('[wp_ulike for="post" style="wpulike-default"]');
}
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<!-- 分页 -->
<div class="pagination">
<?php
echo paginate_links(array(
'total' => $query->max_num_pages,
'current' => $paged,
'format' => '?paged=%#%',
'prev_text' => __('« 上一页'),
'next_text' => __('下一页 »'),
));
?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<h3 style="text-align: center;">你还没有发表碎语噢!</h3>
<p style="text-align: center;">赶快去发表你的第一条碎语心情吧!</p>
<?php endif; ?>
</div>
</main>
</div>
<script type="text/javascript">
$(function() {
var oldClass = "";
var Obj = "";
$(".cbp_tmtimeline li").hover(function() {
Obj = $(this).children(".suiyu_author_img").children("img");
oldClass = Obj.attr("class");
var newClass = oldClass + " zhuan";
Obj.attr("class", newClass);
}, function() {
Obj.attr("class", oldClass);
});
});
</script>
<?php
get_footer();
?>
<!-- RSS 订阅链接 -->
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> 碎语订阅" href="<?php echo esc_url(home_url('/feed/suiyu')); ?>" />
</a>
</div>
然后,建立页面微语菜单,并保存首页导航栏,即可写微语了。
3、archive-suiyu.php文件,可以设置固定拼音suiyi链接,没有我的设置不了,其它拼音可以。为啥这样,我不知道。
<?php
/**
* The template for displaying single Suiyu post.
*/
get_header(); ?>
<style>
/* 在这里加入您的碎语美化样式 */
.cbp_tmtimeline > li {
margin-bottom: 20px;
padding: 15px;
background-color: #FFFFF9;
border-left: 5px solid #E9E3C9;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
border-radius: 8px;
transition: all 0.3s ease;
display: flex;
align-items: center;
}
.cbp_tmtimeline > li:hover {
transform: translateY(-5px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
.suiyu_time {
font-size: 0.9em;
color: #777;
margin-top: 10px;
margin-bottom: 5px;
}
.suiyu_author_img {
margin-right: 15px;
}
.suiyu_author_img img {
border: 1px solid #ddd;
padding: 2px;
float: left;
border-radius: 64px;
transition: all 1.0s;
height: 50px;
}
</style>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="cbp_suiyu">
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<ul class="cbp_tmtimeline">
<li>
<span class="suiyu_author_img">
<img src="<?php echo esc_url(get_avatar_url(get_the_author_meta('ID'))); ?>" class="avatar avatar-48" width="48" height="48">
</span>
<div class="cbp_tmlabel">
<p><?php the_content(); ?></p>
<p class="suiyu_time">
<i class="fa fa-clock-o"></i> <?php echo get_the_time('Y年n月j日G:i'); ?><span class="page-views" style="margin-left: 10px; font-size: 14px; color: #666;">
<?php
if ( function_exists( 'pvc_get_post_views' ) ) {
$page_views = pvc_get_post_views( get_the_ID() );
echo '浏览: ' . $page_views;
}
?>
</span>
</p><?php
if (function_exists('wp_ulike')) {
echo do_shortcode('[wp_ulike for="post" style="wpulike-default"]');
}
?>
</div>
</div>
</p>
</div>
</li>
</ul>
<?php endwhile;
else : ?>
<p>未找到碎语内容。</p>
<?php endif; ?>
</div>
</main>
</div>
<?php
get_footer();
?>
4、single-suiyu.php文件,可以加载碎语单独页面模板。
<?php
/**
* The template for displaying single Suiyu post.
*/
get_header(); ?>
<?php
// 在wp_head中添加关键词和描述
add_action('wp_head', function() {
// 添加关键词
$keywords = '碎语札记, 生活, 记录, 日常感悟, 灵感, 碎片化, 每日灵感碎片';
echo '<meta name="keywords" content="' . esc_attr($keywords) . '">';
// 添加描述
$description = '日常碎语札记,记录生活中的点滴感悟,分享摄影作品和那些转瞬即逝的灵感。不拘泥于形式,不追求完整,只言片语,碎片化生活记录。';
echo '<meta name="description" content="' . esc_attr($description) . '">';
});
?>
<style>
/* 在这里加入您的碎语美化样式 */
.cbp_tmtimeline > li {
margin-bottom: 20px;
padding: 15px;
background-color: #FFFFF9;
border-left: 5px solid #E9E3C9;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
border-radius: 8px;
transition: all 0.3s ease;
display: flex;
align-items: center;
}
.cbp_tmtimeline > li:hover {
transform: translateY(-5px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
.suiyu_time {
font-size: 0.9em;
color: #777;
margin-top: 10px;
margin-bottom: 5px;
}
.suiyu_author_img {
margin-right: 15px;
}
.suiyu_author_img img {
border: 1px solid #ddd;
padding: 2px;
float: left;
border-radius: 64px;
transition: all 1.0s;
height: 50px;
}
</style>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="cbp_suiyu">
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<ul class="cbp_tmtimeline">
<li>
<span class="suiyu_author_img">
<img src="<?php echo esc_url(get_avatar_url(get_the_author_meta('ID'))); ?>" class="avatar avatar-48" width="48" height="48">
</span>
<div class="cbp_tmlabel">
<p><?php the_content(); ?></p>
<p class="suiyu_time">
<i class="fa fa-clock-o"></i> <?php echo get_the_time('Y年n月j日G:i'); ?><span class="page-views" style="margin-left: 10px; font-size: 14px; color: #666;">
<?php
if ( function_exists( 'pvc_get_post_views' ) ) {
$page_views = pvc_get_post_views( get_the_ID() );
echo '阅读: ' . $page_views;
}
?>
</span>
</p><?php
if (function_exists('wp_ulike')) {
echo do_shortcode('[wp_ulike for="post" style="wpulike-default"]');
}
?>
</div>
</div>
</p>
</div>
</li>
</ul>
<?php endwhile;
else : ?>
<p>未找到碎语内容。</p>
<?php endif; ?>
</div>
</main>
</div>
<?php
get_footer();
?>重编2025,2月3号
碎语札记板块,标题添加“碎语|”代码,代码添加模板函数 functions.php文件尾部
// 在保存文章时,自动修改碎语札记文章的标题
function add_prefix_to_suiyu_title($post_id) {
// 确保是保存文章时才进行处理,避免无限循环
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
// 仅在碎语札记('suiyu')文章类型时修改标题
if ('suiyu' == get_post_type($post_id)) {
$post_title = get_post_field('post_title', $post_id);
// 如果标题没有以“碎语|”开头,自动添加
if (strpos($post_title, '碎语 | ') !== 0) {
$new_title = '碎语 | ' . $post_title;
// 更新文章标题
wp_update_post(array(
'ID' => $post_id,
'post_title' => $new_title
));
}
}
return $post_id;
}
// 添加钩子,当文章保存时调用此函数
add_action('save_post', 'add_prefix_to_suiyu_title');
本文由载润札记原创发布,转载请注明出处:WordPress添加说说微语。