wordpress(DUX主题)怎么给文章添加历史上的今天

经常看到一些博主写的文章下方有一个历史上的今天字样,感觉很好,如果你想要这样的效果我们只需要用纯代码即可实现,之前是有一个柳城大佬的 wp-today 插件,不过现在已经下架了,根据该插件里面的代码,做了一些提取,方法往下看,注意是的(该功能需要站点文章一年以上才能有效,因为是历史文章)

方法如下:(依次将下面的代码添加)

1、建立一个module_today_in_history.php 文件,并且将该文件丢到modules文件夹中。代码如下:

<?php
//历史上的今天, WP-Today 插件,该插件已下架,已提取成代码实现
function today_in_history(){
 
$title = QGG_options('today_in_history_title'); // $title = "历史上的今天"; 其他主题用户改成固定值
$limit = QGG_options('today_in_history_num'); // $limit = 5; 其他主题用户改成固定值
 
global $wpdb;
$post_year = get_the_time('Y');
$post_month = get_the_time('m');
$post_day = get_the_time('j');
 
$sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM 
$wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
order by post_date_gmt DESC limit $limit";
$histtory_post = $wpdb->get_results($sql);
if( $histtory_post ){
foreach( $histtory_post as $post ){
$h_year = $post->h_year;
$h_post_title = $post->post_title;
$h_permalink = get_permalink( $post->ID );
$h_comments = $post->comment_count;
$h_post .= "<li>$h_year:&nbsp;&nbsp;<a href='".$h_permalink."' title='Permanent Link to ".$h_post_title."'>$h_post_title <span>($h_comments)</span></a></li>";
}
}
 
if ( $h_post ){
$result = "<section class='history-in-today'><h2>".$title."</h2><div><ul>".$h_post."</ul></div></section>";
}else{
$result = "<section class='history-in-today'><h2>".$title."</h2><div>哇哦~~~,历史上的今天没发表过文章哦</div></section>";
}
 
echo $result;
}
today_in_history();
?>

2、调用前端代码,一般放在single.php文件中,如果是DUX主题可行。

<?php 
// 历史上的今天功能
if( QGG_options('today_in_history_open') ){
include get_stylesheet_directory(). '/diy/modules/module_today_in_history.php'; 
}
?>

3、设置后台选项,方便于编辑设置。一般放在options.php文件中即可。

<?php
/**
*老牛小站文章
*/ 
$options[] = array(
'name' => __('老牛文章', 'QGG'),
'type' => 'heading' );
// 文章页历史上的今天
$options[] = array(
'name' => __('历史上的今天', 'QGG'),
'desc' => __('开启', 'QGG'),
'id' => 'today_in_history_open',
'std' => true,
'type' => 'checkbox');
 
$options[] = array(
'name' => __('历史上的今天-标题文字', 'QGG'),
'desc' => __('左上角的标题文字', 'QGG'),
'id' => 'today_in_history_title',
'std' => __('历史上的今天', 'QGG'),
'type' => 'text');
 
$options[] = array(
'name' => __('历史上的今天-显示文章数', 'QGG'),
'desc' => __('纯数字,显示列表文章数量。不明白?<a href="https://www.jzb85.com">点击这里</a> 进行留言。', 'QGG'),
'id' => 'today_in_history_num',
'std' => 5,
'class' => 'mini',
'type' => 'text');
 
?>

到了这一步基本上已完成,那么感兴趣的朋友可以去试一试,在试之前记得养成好的习惯进行备份,以免造成不必要的文件丢失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DUX主题是大前端当前使用主题,是大前端积累多年Wordpress主题经验设计而成;DUX取名大前端标志“D”和用户体验代名词“UX”,意为大前端主题帮助各位站长实现更好用户体验的目标和决心。扁平的风格和干净白色的架构会让网站显得内涵而出色,DUX主题定能帮助购买者实现快速搭建高质量网站的目的。 演示地址:http://wp.weknow.cn DUX主题1.2版本更新内容: 更新:取消加密,现在可以自己折腾了; 修复:侧边栏偶尔错位问题; 修复:404页面图片不显示; 修复:文章页插入视频自适应宽高; 更新:文章页视频居中展示; 修复:手机端文章页面标题下分类不显示的问题; 更新:文章页面内容分页模块; 修复:多个位置的头像变形; 更新:登录或注册弹框显示后,点击透明遮罩可关闭; 更新:登录或注册弹框显示后,第一个输入框获取焦点,可直接输入; 更新:登录或注册弹框输入框内按CTRL键将自动提交并验证表单; 更新:登录支持用户名和邮箱; 更新:注册时发送密码至注册邮箱,以验证邮箱有效性; 更新:会员中心关于registration.php文件的引用的错误; 更新:会员中心关于PRC和escape的错误; 更新:会员中心修改资料页面调整,邮箱不可修改; 更新:站点标题过长引起的LOGO宽度问题; 更新:去除链接focus时的虚线; 更新:分享模块的文字错位问题;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值