各种网络神器
手机赚钱

WordPress 添加历史上的今天文章列表

【点击赚钱】闪电鸡推出新的模式一天躺赚144块

随着建站时间增长,一些之前写过的文章逐渐被埋没在历史的角落里。为了能更好地展示这些文章,我们可以在文章页面下添加一个“历史上的今天”功能获取老文章列表予以展示。

该功能代码取至柳城大佬的 wp-today 插件,该插件因年久失修已经在 WordPress 下架了。不过我从网上搜到的版本经测试依旧能用,文末会提供下载。在原有插件代码的基础上,我略微调整了下,并针对 OptionsFrameWork 后台框架,做了些设置选项。(注意:历史上的今天功能,仅针对建站一年以上的网站有效,时间太短的网站也就不存在历史上的今天了不是吗?ㄟ( ▔, ▔ )ㄏ )

功能核心文件

新建一个名为 module_today_in_history.php 的文件,然后将以下代码丢到改文件中。

<?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();
?>

前端调用代码

然后在你需要调用的位置添加如下代码,一般是主题的 single.php 文件中。

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

其他主题的话,如果不需要后台设置选项,去掉 if 判断即可。

后台设置选项

最后,对于使用 OptionsFramework 框架的主题用户,可在 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://fulioh.com">点击这里</a> 进行留言。', 'QGG'),
		'id' => 'today_in_history_num',
		'std' => 5,
		'class' => 'mini',
		'type' => 'text');			

?>

样式文件的话没有调整,大家根据自己喜好自行调整一下吧。

英文:

As the time for building stations increased, some previously written articles were gradually buried in the corner of history. In order to better display these articles, we can add a “History Today” feature under the article page to get a list of old articles to display.

The function code is taken to the wp-today plugin of Liucheng Daxie, which has been removed from WordPress due to disrepair. However, the version I found on the Internet has been tested and still available, and will be available for download at the end of the article. Based on the original plug-in code, I slightly adjusted it and made some setting options for the OptionsFrameWork back-end framework. (Note: Today’s functions in history are only valid for websites that have been built for more than one year. Websites that are too short in time are not in history today. Isn’t it? ㄟ( ▔, ▔ )ㄏ )

Functional core file
Create a new file called module_today_in_history.php and drop the following code into the file.

<?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();
?>

Front-end calling code
Then add the following code to the location you need to call, usually in the theme’s single.php file.

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

For other topics, if you don’t need the background setting option, remove the if judgment.

Background setting options
Finally, for theme users who use the OptionsFramework framework, add the following code to set the background options in the options.php file.

<?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://fulioh.com">点击这里</a> 进行留言。', 'QGG'),
		'id' => 'today_in_history_num',
		'std' => 5,
		'class' => 'mini',
		'type' => 'text');			

?>

The style file is not adjusted, so please adjust it according to your own preferences.

赞(0)
未经允许不得转载:920神器网 » WordPress 添加历史上的今天文章列表

评论 抢沙发

评论前必须登录!

立即登录   注册