如你所见,这个博客就是我用来写一些乱七八糟的东西,但是我保证不会犯法,就问你怕不怕。
环境
在阿里云弄的CentOS8环境,不过只有1C2G。安装了nginx 1.20.2,mysql 8.0.26,worpress 5.9。
主题
我使用的主题是wordpress自带的GeneratePress主题。
我通过main.min.css样式表调整了主题的字体以及通过小工具定义了右侧栏。感觉这个主题是我比较喜欢的,简洁而且配置多。
头像
头像主要是关于评论的头像,首先安装code snippets插件,然后在其中增加片段(代码实际上和在function.php中添加一样,但是code snippets插件方便管理),同时选择了插件Enlighter和Crayon Syntax Highlighter进行高亮显示和编辑。
add_filter( 'get_avatar' , 'local_random_avatar' , 1 , 5 );
function local_random_avatar( $avatar, $id_or_email, $size, $default, $alt) {
if ( ! empty( $id_or_email->user_id ) ) {
$avatar = ''.get_template_directory_uri().'/avatar/admin.jpg';
}else{
$random = mt_rand(1, 10);
$avatar = ''.get_template_directory_uri().'/avatar/'. $random .'.jpg';
}
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' style='height:30px; width:30px' />";
return $avatar;
}
另外再增加默认设置文章中第一幅图为特色图:
function autoset_featured_image()
{
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1");
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('the_post', 'autoset_featured_image');
add_action('save_post', 'autoset_featured_image');
add_action('draft_to_publish', 'autoset_featured_image');
add_action('new_to_publish', 'autoset_featured_image');
add_action('pending_to_publish', 'autoset_featured_image');
add_action('future_to_publish', 'autoset_featured_image');
主题目录中新建一个名称为avatar的文件夹,放了几张头像图片。
添加阅读时长功能:
// 阅读时间代码
function wp_get_reading_time($content) {
$zm_format = '<span class="reading-time">  时长%min%分%sec%秒</span>';
$zm_chars_per_minute = 300; // 估算1分种阅读字数
$zm_format = str_replace('%num%', $zm_chars_per_minute, $zm_format);
$words = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($content))),'UTF-8');
$minutes = floor($words / $zm_chars_per_minute);
$seconds = floor($words % $zm_chars_per_minute / ($zm_chars_per_minute / 60));
return str_replace('%sec%', $seconds, str_replace('%min%', $minutes, $zm_format));
}
function wp_reading_time() {
echo wp_get_reading_time(get_the_content());
}
在主题为GeneratePress时,找到post_meta.php增加如下代码:
if ( 'counter' === $item ) {
$schema_type = generate_get_schema_type();
$byline = '<span class="byline"> </span> ';
echo apply_filters(
'generate_post_author_output',
sprintf(
$byline,
apply_filters( 'generate_inside_post_meta_item_output', '', 'author' )
)
);
if(function_exists('the_views')) { the_views(); } //WP-PostViews插件
if(function_exists('wp_reading_time')) {wp_reading_time();}
}
//generate_get_header_entry_meta_items中添加counter
function generate_get_header_entry_meta_items() {
$items = apply_filters(
'generate_header_entry_meta_items',
array(
'date',
'author',
'counter',
)
);
// Disable post meta items based on their individual filters.
$items = generate_disable_post_meta_items( $items );
return $items;
}
插件
主要用到的插件有:
- Add Form Server,用于添加大图。
- Code Snippets,用于方便function.php添加功能。
- Crayon Syntax Highlighter,代码高亮。
- Enlighter-Customizable Syntax Highlighter,多合一语法高亮。
- Table of Contents Plus,自动生成目录。
- WP Last Modified Info,自动生成文章最后修改时间。
- WP-PostViews,显示文章查看次数。
- Default featured image,文章默认设置hero图片。
最后更新于 2023-11-08 16:52:49 by twotwolucky
似懂非懂
darknet site tor markets darknet drug links