\u003c?php
/**
* Lemon主题核心功能
*
* @package Lemon
*/
/**
* 主题设置
*/
function lemon_setup() {
// 添加语言支持
load_theme_textdomain( 'lemon', get_template_directory() . '/languages' );
// 添加默认帖子和评论RSS源链接到头部
add_theme_support( 'automatic-feed-links' );
// 添加标题标签支持
add_theme_support( 'title-tag' );
// 添加缩略图支持
add_theme_support( 'post-thumbnails' );
// 设置内容宽度
if ( ! isset( $content_width ) ) {
$content_width = 900;
}
// 注册导航菜单
register_nav_menus( array(
'menu-1' =\u003e esc_html__( 'Primary', 'lemon' ),
'footer-menu' =\u003e esc_html__( 'Footer Menu', 'lemon' ),
'social-menu' =\u003e esc_html__( 'Social Menu', 'lemon' ),
) );
// 添加HTML5支持
add_theme_support( 'html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
) );
// 添加post formats支持
add_theme_support( 'post-formats', array(
'aside',
'gallery',
'link',
'image',
'quote',
'status',
'video',
'audio',
'chat',
) );
// 添加自定义背景支持
add_theme_support( 'custom-background', apply_filters( 'lemon_custom_background_args', array(
'default-color' =\u003e 'ffffff',
'default-image' =\u003e '',
) ) );
// 添加自定义Logo支持
add_theme_support( 'custom-logo', array(
'height' =\u003e 60,
'width' =\u003e 200,
'flex-height' =\u003e true,
'flex-width' =\u003e true,
) );
}
add_action( 'after_setup_theme', 'lemon_setup' );
/**
* 注册侧边栏
*/
function lemon_widgets_init() {
// 左侧边栏
register_sidebar( array(
'name' =\u003e esc_html__( 'Left Sidebar', 'lemon' ),
'id' =\u003e 'left-sidebar',
'description' =\u003e esc_html__( 'Add widgets here.', 'lemon' ),
'before_widget' =\u003e '\u003csection id="%1$s" class="widget %2$s"\u003e',
'after_widget' =\u003e '\u003c/section\u003e',
'before_title' =\u003e '\u003ch2 class="widget-title"\u003e',
'after_title' =\u003e '\u003c/h2\u003e',
) );
// 右侧边栏
register_sidebar( array(
'name' =\u003e esc_html__( 'Right Sidebar', 'lemon' ),
'id' =\u003e 'right-sidebar',
'description' =\u003e esc_html__( 'Add widgets here.', 'lemon' ),
'before_widget' =\u003e '\u003csection id="%1$s" class="widget %2$s"\u003e',
'after_widget' =\u003e '\u003c/section\u003e',
'before_title' =\u003e '\u003ch2 class="widget-title"\u003e',
'after_title' =\u003e '\u003c/h2\u003e',
) );
// 页脚小工具区域
register_sidebar( array(
'name' =\u003e esc_html__( 'Footer Widget Area', 'lemon' ),
'id' =\u003e 'footer-widget-area',
'description' =\u003e esc_html__( 'Add widgets here.', 'lemon' ),
'before_widget' =\u003e '\u003csection id="%1$s" class="widget %2$s"\u003e',
'after_widget' =\u003e '\u003c/section\u003e',
'before_title' =\u003e '\u003ch2 class="widget-title"\u003e',
'after_title' =\u003e '\u003c/h2\u003e',
) );
}
add_action( 'widgets_init', 'lemon_widgets_init' );
/**
* 注册微语分类ID常量
*/
if ( ! defined( 'LEMON_MICROBLOG_CATEGORY_ID' ) ) {
define( 'LEMON_MICROBLOG_CATEGORY_ID', 21 );
}
/**
* 计算网站运行天数
*/
function lemon_get_blog_age() {
$start_date = '2025-03-29';
$start = new DateTime( $start_date );
$now = new DateTime();
$interval = $start-\u003ediff( $now );
return $interval-\u003edays;
}
/**
* 获取长文数量(排除微语分类)
*/
function lemon_get_regular_post_count() {
$args = array(
'category__not_in' => array( LEMON_MICROBLOG_CATEGORY_ID ),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
$count = $query->found_posts;
wp_reset_postdata();
return $count;
}
'category__not_in' =\u003e array( LEMON_MICROBLOG_CATEGORY_ID ),
'post_type' =\u003e 'post',
'post_status' =\u003e 'publish',
'posts_per_page' =\u003e -1
);
$query = new WP_Query( $args );
$count = $query-\u003efound_posts;
wp_reset_postdata();
return $count;
}
/**
* 获取微语数量
*/
function lemon_get_microblog_count() {
$args = array(
'category__in' =\u003e array( LEMON_MICROBLOG_CATEGORY_ID ),
'post_type' =\u003e 'post',
'post_status' =\u003e 'publish',
'posts_per_page' =\u003e -1
);
$query = new WP_Query( $args );
$count = $query-\u003efound_posts;
wp_reset_postdata();
return $count;
}
/**
* 获取评论数量
*/
function lemon_get_comment_count() {
global $wpdb;
$count = $wpdb-\u003eget_var( "SELECT COUNT(*) FROM $wpdb-\u003ecomments WHERE comment_approved = 1" );
return $count;
}
/**
* 获取友链数量
*/
function lemon_get_link_count() {
$links = get_bookmarks( array(
'category' =\u003e null,
'orderby' =\u003e 'name',
'order' =\u003e 'ASC'
) );
return count( $links );
}
/**
* 获取光阴似箭数据
*/
function lemon_get_time_remaining() {
$now = new DateTime();
$current_year = (int) $now-\u003eformat( 'Y' );
$current_week = (int) $now-\u003eformat( 'W' );
$current_month = (int) $now-\u003eformat( 'm' );
// 距离本周结束
$week_end = new DateTime();
$week_end-\u003emodify( 'Sunday this week' );
$week_remaining = $now-\u003ediff( $week_end );
// 距离本月结束
$month_end = new DateTime();
$month_end-\u003emodify( 'last day of this month' );
$month_remaining = $now-\u003ediff( $month_end );
// 距离年底
$year_end = new DateTime( "$current_year-12-31" );
$year_remaining = $now-\u003ediff( $year_end );
return array(
'week' =\u003e $week_remaining-\u003edays,
'month' =\u003e $month_remaining-\u003edays,
'year' =\u003e $year_remaining-\u003edays
);
}
/**
* 自定义评论模板
*/
function lemon_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
?\u003e
\u003cli \u003c?php comment_class(); ?\u003e id="li-comment-\u003c?php comment_ID(); ?\u003e"\u003e
\u003cdiv id="comment-\u003c?php comment_ID(); ?\u003e" class="comment-body"\u003e
\u003cdiv class="comment-author vcard"\u003e
\u003c?php echo get_avatar( $comment, 40 ); ?\u003e
\u003cb class="fn"\u003e\u003c?php comment_author_link(); ?\u003e\u003c/b\u003e
\u003cspan class="comment-meta commentmetadata"\u003e
\u003c?php comment_date(); ?\u003e \u003c?php comment_time(); ?\u003e
\u003c/span\u003e
\u003c/div\u003e
\u003c?php if ( $comment-\u003ecomment_approved == '0' ) : ?\u003e
\u003cem class="comment-awaiting-moderation"\u003e\u003c?php esc_html_e( 'Your comment is awaiting moderation.', 'lemon' ); ?\u003e\u003c/em\u003e
\u003cbr /\u003e
\u003c?php endif; ?\u003e
\u003cdiv class="comment-content"\u003e\u003c?php comment_text(); ?\u003e\u003c/div\u003e
\u003cdiv class="reply"\u003e
\u003c?php comment_reply_link( array_merge( $args, array( 'depth' =\u003e $depth, 'max_depth' =\u003e $args['max_depth'] ) ) ); ?\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
}
/**
* 自定义微语评论模板
*/
function lemon_microblog_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
?\u003e
\u003cli \u003c?php comment_class(); ?\u003e id="li-comment-\u003c?php comment_ID(); ?\u003e"\u003e
\u003cdiv id="comment-\u003c?php comment_ID(); ?\u003e" class="microblog-comment-body"\u003e
\u003cdiv class="comment-content"\u003e\u003c?php comment_text(); ?\u003e\u003c/div\u003e
\u003cdiv class="comment-meta"\u003e
\u003cspan class="comment-author"\u003e\u003c?php comment_author_link(); ?\u003e\u003c/span\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
}
/**
* 自定义评论表单
*/
function lemon_comment_form_defaults( $defaults ) {
$defaults['comment_field'] = '
\u003cdiv class="form-group"\u003e
\u003clabel for="comment"\u003e\u003c?php esc_html_e( 'Comment', 'lemon' ); ?\u003e \u003cspan class="required"\u003e*\u003c/span\u003e\u003c/label\u003e
\u003ctextarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"\u003e\u003c/textarea\u003e
\u003c/div\u003e
';
$defaults['fields']['author'] = '
\u003cdiv class="form-group"\u003e
\u003clabel for="author"\u003e\u003c?php esc_html_e( 'Name', 'lemon' ); ?\u003e \u003cspan class="required"\u003e*\u003c/span\u003e\u003c/label\u003e
\u003cinput id="author" name="author" type="text" value="\u003c?php echo esc_attr( $commenter['comment_author'] ); ?\u003e" size="30" maxlength="245" required="required"\u003e
\u003c/div\u003e
';
$defaults['fields']['email'] = '
\u003cdiv class="form-group"\u003e
\u003clabel for="email"\u003e\u003c?php esc_html_e( 'Email', 'lemon' ); ?\u003e \u003cspan class="required"\u003e*\u003c/span\u003e\u003c/label\u003e
\u003cinput id="email" name="email" type="email" value="\u003c?php echo esc_attr( $commenter['comment_author_email'] ); ?\u003e" size="30" maxlength="100" aria-describedby="email-notes" required="required"\u003e
\u003c/div\u003e
';
$defaults['fields']['url'] = '
\u003cdiv class="form-group"\u003e
\u003clabel for="url"\u003e\u003c?php esc_html_e( 'Website', 'lemon' ); ?\u003e\u003c/label\u003e
\u003cinput id="url" name="url" type="url" value="\u003c?php echo esc_attr( $commenter['comment_author_url'] ); ?\u003e" size="30" maxlength="200"\u003e
\u003c/div\u003e
';
$defaults['submit_button'] = '
\u003cbutton name="submit" type="submit" id="submit" class="submit-btn"\u003e\u003c?php esc_html_e( 'Post Comment', 'lemon' ); ?\u003e\u003c/button\u003e
';
return $defaults;
}
add_filter( 'comment_form_defaults', 'lemon_comment_form_defaults' );
/**
* 注册自定义小工具
*/
function lemon_register_widgets() {
// 博主信息小工具
class Lemon_Author_Info_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_author_info',
__( '博主信息', 'lemon' ),
array( 'description' =\u003e __( '显示博主信息', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="author-info-widget"\u003e
\u003cdiv class="author-avatar"\u003e
\u003cimg src="https://tuchuang.boke.lu/%E5%8D%9A%E5%AE%A2%E5%BD%95/2024%E5%B9%B4%E7%94%9F%E6%B4%BB%E7%B1%BBlogo/Andy%E7%83%A7%E9%BA%A6.png" alt="博主头像" class="avatar"\u003e
\u003cdiv class="author-details"\u003e
\u003cdiv class="author-name"\u003eVss\u003c/div\u003e
\u003cdiv class="author-domain"\u003eVss.Plus\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="author-bio"\u003e记录美好生活\u003c/div\u003e
\u003cdiv class="author-status"\u003e博主近期动态和心情\u003c/div\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '博主信息', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 最近在看的书小工具
class Lemon_Current_Book_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_current_book',
__( '最近在看的书', 'lemon' ),
array( 'description' =\u003e __( '显示最近在看的书', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="current-book-widget"\u003e
\u003cdiv class="book-cover"\u003e
\u003cimg src="" alt="书籍封面" class="book-image"\u003e
\u003c/div\u003e
\u003cdiv class="book-info"\u003e
\u003ch4 class="book-title"\u003e书籍标题\u003c/h4\u003e
\u003cp class="book-description"\u003e书籍简介内容...\u003c/p\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '最近在看的书', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 进度表小工具
class Lemon_Progress_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_progress',
__( '进度表', 'lemon' ),
array( 'description' =\u003e __( '显示近期任务进度', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="progress-widget"\u003e
\u003cdiv class="progress-item"\u003e
\u003cdiv class="progress-label"\u003e完善主题\u003c/div\u003e
\u003cdiv class="progress-bar"\u003e
\u003cdiv class="progress-fill" style="width: 70%;"\u003e\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="progress-item"\u003e
\u003cdiv class="progress-label"\u003e养成早睡习惯\u003c/div\u003e
\u003cdiv class="progress-bar"\u003e
\u003cdiv class="progress-fill" style="width: 45%;"\u003e\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="progress-item"\u003e
\u003cdiv class="progress-label"\u003e养成记账习惯\u003c/div\u003e
\u003cdiv class="progress-bar"\u003e
\u003cdiv class="progress-fill" style="width: 60%;"\u003e\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '进度表', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 光阴似箭小工具
class Lemon_Time_Remaining_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_time_remaining',
__( '光阴似箭', 'lemon' ),
array( 'description' =\u003e __( '显示距离各个时间点的剩余天数', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$time_data = lemon_get_time_remaining();
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="time-remaining-widget"\u003e
\u003cdiv class="time-item"\u003e
\u003cdiv class="time-label"\u003e距离本周结束\u003c/div\u003e
\u003cdiv class="time-value"\u003e\u003c?php echo $time_data['week']; ?\u003e 天\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="time-item"\u003e
\u003cdiv class="time-label"\u003e距离本月结束\u003c/div\u003e
\u003cdiv class="time-value"\u003e\u003c?php echo $time_data['month']; ?\u003e 天\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="time-item"\u003e
\u003cdiv class="time-label"\u003e距离年底结束\u003c/div\u003e
\u003cdiv class="time-value"\u003e\u003c?php echo $time_data['year']; ?\u003e 天\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '光阴似箭', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 每日一言小工具
class Lemon_Daily_Quote_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_daily_quote',
__( '每日一言', 'lemon' ),
array( 'description' =\u003e __( '显示每日一言', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="daily-quote-widget"\u003e
\u003cdiv class="quote-image"\u003e
\u003cimg src="https://blogimg.lieme.cn/2025/06/2025062301502041.png" alt="每日一言" class="quote-img"\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '每日一言', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 最新文章小工具
class Lemon_Latest_Posts_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_latest_posts',
__( '最新文章', 'lemon' ),
array( 'description' =\u003e __( '显示最新文章', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="latest-posts-widget"\u003e
\u003c?php
$args = array(
'category__not_in' =\u003e array( LEMON_MICROBLOG_CATEGORY_ID ),
'posts_per_page' =\u003e 4,
'post_status' =\u003e 'publish'
);
$query = new WP_Query( $args );
if ( $query-\u003ehave_posts() ) :
while ( $query-\u003ehave_posts() ) : $query-\u003ethe_post();
?\u003e
\u003cdiv class="post-item"\u003e
\u003cdiv class="post-title"\u003e\u003ca href="\u003c?php the_permalink(); ?\u003e"\u003e\u003c?php the_title(); ?\u003e\u003c/a\u003e\u003c/div\u003e
\u003c?php if ( has_post_thumbnail() ) : ?\u003e
\u003cdiv class="post-thumbnail"\u003e\u003ca href="\u003c?php the_permalink(); ?\u003e"\u003e\u003c?php the_post_thumbnail( 'thumbnail' ); ?\u003e\u003c/a\u003e\u003c/div\u003e
\u003c?php endif; ?\u003e
\u003c/div\u003e
\u003c?php
endwhile;
wp_reset_postdata();
endif;
?\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '最新文章', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 最新微语评论小工具
class Lemon_Latest_Microblog_Comments_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_latest_microblog_comments',
__( '最新微语评论', 'lemon' ),
array( 'description' =\u003e __( '显示微语的最新评论', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="latest-microblog-comments-widget"\u003e
\u003c?php
// 获取微语文章ID
$microblog_args = array(
'category__in' =\u003e array( LEMON_MICROBLOG_CATEGORY_ID ),
'posts_per_page' =\u003e -1,
'fields' =\u003e 'ids'
);
$microblog_query = new WP_Query( $microblog_args );
$microblog_ids = $microblog_query-\u003eposts;
// 获取这些文章的评论
if ( ! empty( $microblog_ids ) ) {
$comments_args = array(
'post__in' =\u003e $microblog_ids,
'number' =\u003e 4,
'status' =\u003e 'approve',
'orderby' =\u003e 'comment_date_gmt',
'order' =\u003e 'DESC'
);
$comments = get_comments( $comments_args );
if ( $comments ) :
foreach ( $comments as $comment ) :
?\u003e
\u003cdiv class="comment-item"\u003e
\u003cdiv class="comment-content"\u003e\u003c?php echo get_comment_text( $comment-\u003ecomment_ID ); ?\u003e\u003c/div\u003e
\u003cdiv class="comment-author"\u003e\u003c?php echo get_comment_author( $comment-\u003ecomment_ID ); ?\u003e\u003c/div\u003e
\u003c/div\u003e
\u003c?php
endforeach;
endif;
}
wp_reset_postdata();
?\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '最新微语评论', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
// 站点信息小工具
class Lemon_Site_Info_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'lemon_site_info',
__( '站点信息', 'lemon' ),
array( 'description' =\u003e __( '显示站点统计信息', 'lemon' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?\u003e
\u003cdiv class="site-info-widget"\u003e
\u003cdiv class="info-item"\u003e
\u003cdiv class="info-label"\u003e运行天数\u003c/div\u003e
\u003cdiv class="info-value"\u003e\u003c?php echo lemon_get_blog_age(); ?\u003e 天\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="info-item"\u003e
\u003cdiv class="info-label"\u003e长文数\u003c/div\u003e
\u003cdiv class="info-value"\u003e\u003c?php echo lemon_get_regular_post_count(); ?\u003e 篇\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="info-item"\u003e
\u003cdiv class="info-label"\u003e微语数\u003c/div\u003e
\u003cdiv class="info-value"\u003e\u003c?php echo lemon_get_microblog_count(); ?\u003e 条\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="info-item"\u003e
\u003cdiv class="info-label"\u003e评论数\u003c/div\u003e
\u003cdiv class="info-value"\u003e\u003c?php echo lemon_get_comment_count(); ?\u003e 条\u003c/div\u003e
\u003c/div\u003e
\u003cdiv class="info-item"\u003e
\u003cdiv class="info-label"\u003e友链数\u003c/div\u003e
\u003cdiv class="info-value"\u003e\u003c?php echo lemon_get_link_count(); ?\u003e 个\u003c/div\u003e
\u003c/div\u003e
\u003c/div\u003e
\u003c?php
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance['title'] ) ) {
$title = $instance['title'];
} else {
$title = __( '站点信息', 'lemon' );
}
?\u003e
\u003cp\u003e
\u003clabel for="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e"\u003e\u003c?php _e( 'Title:' ); ?\u003e\u003c/label\u003e
\u003cinput class="widefat" id="\u003c?php echo $this-\u003efield_id( 'title' ); ?\u003e" name="\u003c?php echo $this-\u003efield_name( 'title' ); ?\u003e" type="text" value="\u003c?php echo esc_attr( $title ); ?\u003e"\u003e
\u003c/p\u003e
\u003c?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
register_widget( 'Lemon_Author_Info_Widget' );
register_widget( 'Lemon_Current_Book_Widget' );
register_widget( 'Lemon_Progress_Widget' );
register_widget( 'Lemon_Time_Remaining_Widget' );
register_widget( 'Lemon_Daily_Quote_Widget' );
register_widget( 'Lemon_Latest_Posts_Widget' );
register_widget( 'Lemon_Latest_Microblog_Comments_Widget' );
register_widget( 'Lemon_Site_Info_Widget' );
}
add_action( 'widgets_init', 'lemon_register_widgets' );
/**
* 自定义模板加载顺序
*/
function lemon_template_path() {
return LEMON_TEMPLATE_PATH;
}
/**
* 添加自定义模板路径
*/
function lemon_add_template_locations( $template_dirs ) {
$template_dirs[] = get_template_directory() . '/template-parts';
return $template_dirs;
}
add_filter( 'theme_file_path', 'lemon_add_template_locations' );
/**
* 加载评论模板
*/
function lemon_comments_template( $template ) {
$post = get_post();
if ( $post && has_category( LEMON_MICROBLOG_CATEGORY_ID, $post ) ) {
$template = get_template_directory() . '/comments-microblog.php';
}
return $template;
}
add_filter( 'comments_template', 'lemon_comments_template' );
/**
* 移除WordPress版本信息
*/
function lemon_remove_version_info() {
return '';
}
add_filter( 'the_generator', 'lemon_remove_version_info' );
/**
* 添加自定义CSS
*/
function lemon_enqueue_styles() {
wp_enqueue_style( 'lemon-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'lemon_enqueue_styles' );
/**
* 添加自定义JavaScript
*/
function lemon_enqueue_scripts() {
wp_enqueue_script( 'lemon-script', get_template_directory_uri() . '/js/script.js', array( 'jquery' ), null, true );
}
add_action( 'wp_enqueue_scripts', 'lemon_enqueue_scripts' );
/**
* 自定义登录页面样式
*/
function lemon_login_stylesheet() {
wp_enqueue_style( 'custom-login', get_template_directory_uri() . '/style-login.css' );
}
add_action( 'login_enqueue_scripts', 'lemon_login_stylesheet' );
/**
* 修改登录页面logo链接
*/
function lemon_login_logo_url() {
return home_url();
}
add_filter( 'login_headerurl', 'lemon_login_logo_url' );
/**
* 修改登录页面logo标题
*/
function lemon_login_logo_url_title() {
return get_bloginfo( 'name' );
}
add_filter( 'login_headertitle', 'lemon_login_logo_url_title' );
/**
* 自定义搜索表单
*/
function lemon_search_form( $form ) {
$form = '\n\u003cform role="search" method="get" class="search-form" action="' . home_url( '/' ) . '" \u003e\n'
. '\t\u003cinput type="search" class="search-field" placeholder="' . esc_attr_x( '搜索...', 'placeholder', 'lemon' ) . '" value="' . get_search_query() . '" name="s" /\u003e\n'
. '\t\u003cbutton type="submit" class="search-submit"\u003e\u003cspan class="icon icon-search"\u003e\u003c/span\u003e\u003c/button\u003e\n'
. '\u003c/form\n';
return $form;
}
add_filter( 'get_search_form', 'lemon_search_form' );
/**
* 获取文章中的第一张图片作为缩略图
*/
function lemon_get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all( '/\u003cimg.+src=[\'"]([^\'"]+)[\'"]+/i', $post-\u003epost_content, $matches );
$first_img = $matches[1][0];
if ( empty( $first_img ) ) {
$first_img = get_template_directory_uri() . '/images/default-thumbnail.jpg';
}
return $first_img;
}
/**
* 清理WordPress头部多余的代码
*/
function lemon_cleanup_head() {
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
}
add_action( 'init', 'lemon_cleanup_head' );
/**
* 启用短代码在文本小工具中
*/
add_filter( 'widget_text', 'do_shortcode' );
/**
* 自定义分页链接
*/
function lemon_pagination_args( $args ) {
$args['prev_text'] = '\u003cspan class="icon icon-prev"\u003e\u003c/span\u003e 上一页';
$args['next_text'] = '下一页 \u003cspan class="icon icon-next"\u003e\u003c/span\u003e';
$args['type'] = 'plain';
return $args;
}
add_filter( 'paginate_links', 'lemon_pagination_args' );
/**
* 添加自定义页面模板
*/
function lemon_add_page_templates( $templates ) {
$templates['page-fullwidth.php'] = __( 'Full Width Page', 'lemon' );
$templates['page-microblog.php'] = __( 'Microblog Page', 'lemon' );
return $templates;
}
add_filter( 'theme_page_templates', 'lemon_add_page_templates' );
/**
* 自定义错误页面
*/
function lemon_custom_404() {
if ( is_404() ) {
header( 'HTTP/1.1 404 Not Found' );
header( 'Status: 404 Not Found' );
title_tag( '404: 页面未找到' );
}
}
add_action( 'template_redirect', 'lemon_custom_404' );
/**
* 优化数据库查询
*/
function lemon_pre_get_posts( $query ) {
if ( ! is_admin() && $query-\u003eis_main_query() ) {
// 仅获取需要的字段
$query-\u003eset( 'update_post_term_cache', false );
$query-\u003eset( 'update_post_meta_cache', false );
// 限制postmeta查询
if ( $query-\u003eis_search() ) {
$query-\u003eset( 'posts_per_page', 10 );
}
}
return $query;
}
add_action( 'pre_get_posts', 'lemon_pre_get_posts' );
/**
* 自定义文章类型 - 微语
*/
function lemon_create_microblog_post_type() {
$labels = array(
'name' =\u003e _x( '微语', 'Post Type General Name', 'lemon' ),
'singular_name' =\u003e _x( '微语', 'Post Type Singular Name', 'lemon' ),
'menu_name' =\u003e __( '微语', 'lemon' ),
'name_admin_bar' =\u003e __( '微语', 'lemon' ),
'archives' =\u003e __( '微语归档', 'lemon' ),
'attributes' =\u003e __( '微语属性', 'lemon' ),
'parent_item_colon' =\u003e __( '父级微语:', 'lemon' ),
'all_items' =\u003e __( '所有微语', 'lemon' ),
'add_new_item' =\u003e __( '添加新微语', 'lemon' ),
'add_new' =\u003e __( '添加新微语', 'lemon' ),
'new_item' =\u003e __( '新微语', 'lemon' ),
'edit_item' =\u003e __( '编辑微语', 'lemon' ),
'update_item' =\u003e __( '更新微语', 'lemon' ),
'view_item' =\u003e __( '查看微语', 'lemon' ),
'view_items' =\u003e __( '查看微语', 'lemon' ),
'search_items' =\u003e __( '搜索微语', 'lemon' ),
'not_found' =\u003e __( '未找到', 'lemon' ),
'not_found_in_trash' =\u003e __( '回收站中未找到', 'lemon' ),
'featured_image' =\u003e __( '特色图片', 'lemon' ),
'set_featured_image' =\u003e __( '设置特色图片', 'lemon' ),
'remove_featured_image' =\u003e __( '移除特色图片', 'lemon' ),
'use_featured_image' =\u003e __( '使用特色图片', 'lemon' ),
'insert_into_item' =\u003e __( '插入到微语', 'lemon' ),
'uploaded_to_this_item' =\u003e __( '上传到本微语', 'lemon' ),
'items_list' =\u003e __( '微语列表', 'lemon' ),
'items_list_navigation' =\u003e __( '微语列表导航', 'lemon' ),
'filter_items_list' =\u003e __( '过滤微语列表', 'lemon' ),
);
$args = array(
'label' =\u003e __( '微语', 'lemon' ),
'description' =\u003e __( '简短的微语内容', 'lemon' ),
'labels' =\u003e $labels,
'supports' =\u003e array( 'title', 'editor', 'author', 'comments', 'revisions', 'thumbnail' ),
'hierarchical' =\u003e false,
'public' =\u003e true,
'show_ui' =\u003e true,
'show_in_menu' =\u003e true,
'menu_position' =\u003e 5,
'menu_icon' =\u003e 'dashicons-admin-post',
'show_in_admin_bar' =\u003e true,
'show_in_nav_menus' =\u003e true,
'can_export' =\u003e true,
'has_archive' =\u003e true,
'exclude_from_search' =\u003e false,
'publicly_queryable' =\u003e true,
'capability_type' =\u003e 'post',
);
register_post_type( 'microblog', $args );
}
add_action( 'init', 'lemon_create_microblog_post_type' );
/**
* 自定义分类法 - 微语分类
*/
function lemon_create_microblog_taxonomy() {
$labels = array(
'name' =\u003e _x( '微语分类', 'taxonomy general name', 'lemon' ),
'singular_name' =\u003e _x( '微语分类', 'taxonomy singular name', 'lemon' ),
'search_items' =\u003e __( '搜索微语分类', 'lemon' ),
'all_items' =\u003e __( '所有微语分类', 'lemon' ),
'parent_item' =\u003e __( '父级微语分类', 'lemon' ),
'parent_item_colon' =\u003e __( '父级微语分类:', 'lemon' ),
'edit_item' =\u003e __( '编辑微语分类', 'lemon' ),
'update_item' =\u003e __( '更新微语分类', 'lemon' ),
'add_new_item' =\u003e __( '添加新微语分类', 'lemon' ),
'new_item_name' =\u003e __( '新微语分类名称', 'lemon' ),
'menu_name' =\u003e __( '微语分类', 'lemon' ),
);
$args = array(
'hierarchical' =\u003e true,
'labels' =\u003e $labels,
'show_ui' =\u003e true,
'show_admin_column' =\u003e true,
'query_var' =\u003e true,
'rewrite' =\u003e array( 'slug' =\u003e 'microblog-category' ),
'public' =\u003e true,
'show_in_nav_menus' =\u003e true,
'show_tagcloud' =\u003e true,
);
register_taxonomy( 'microblog_category', array( 'microblog' ), $args );
}
add_action( 'init', 'lemon_create_microblog_taxonomy' );
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/vss/wp-content/themes/lemon/functions.php:1) in /www/wwwroot/vss/wp-includes/sitemaps/class-wp-sitemaps-renderer.php on line 126
https://vss.plus/wp-sitemap-posts-post-1.xmlhttps://vss.plus/wp-sitemap-posts-page-1.xmlhttps://vss.plus/wp-sitemap-taxonomies-category-1.xmlhttps://vss.plus/wp-sitemap-taxonomies-post_format-1.xmlhttps://vss.plus/wp-sitemap-users-1.xml