WordPress 调用最近更新过的文章教程 WP 主题优化增强 function 文件实用功能代码段

ximagine
ximagine
站长
2082
文章
1
粉丝
教程 说明书评论89字数 313阅读1分2秒阅读模式
宝藏摘要WordPress程序调用文章的时候,可能会有见过调用最新文章、热门文章,以及多少天热度文章的列表。但是,如果有一些文章是我们早期更新的,近期如果有变动重新编辑过的,这类的文章也是...
荒岛广告位招商进行中...
WordPress 调用最近更新过的文章教程 WP 主题优化增强 function 文件实用功能代码段
日期:2023年8月30日 分类:教程  说明书 评论:发表 浏览:89

WordPress 程序调用文章的时候,可能会有见过调用最新文章、热门文章以及多少天热度文章的列表,但是如果有一些文章是我们早期更新的,近期如果有变动重新编辑过的,这类的文章也是有需要重弄新作为编辑文章调用出来,这样可以告知用户这篇文章已经更新,荒岛本次带来 WordPress 调用最近更新过的文章的教程,只要将下面的代码添加到主题的「functions.php」文件即可。

// 荒岛一座藏有宝藏的小岛
function recently_updated_posts($num=10,$days=7) {
	if( !$recently_updated_posts = get_option('recently_updated_posts') ) {
		query_posts('post_status=publish&orderby=modified&posts_per_page=-1');
		$i=0;
		while ( have_posts() && $i<$num ) : the_post();
			if (current_time('timestamp') - get_the_time('U') > 60*60*24*$days) {
				$i++;
				$the_title_value=get_the_title();
				$recently_updated_posts.='<li><a href="'.get_permalink().'" title="'.$the_title_value.'">'
				.$the_title_value.'</a></li>';
			}
		endwhile;
		wp_reset_query();
		if ( !empty($recently_updated_posts) ) update_option('recently_updated_posts', $recently_updated_posts);
	}
	$recently_updated_posts=($recently_updated_posts == '') ? '<li>宝藏目前 没有更新</li>' : $recently_updated_posts;
	echo $recently_updated_posts;
}
function clear_cache_recently() {
	update_option('recently_updated_posts', '');
}
add_action('save_post', 'clear_cache_recently');
宝藏内容来自荒岛-https://x-imagine.com/wordpress-call-recently-updated-articles.html宝藏内容来自荒岛-https://x-imagine.com/wordpress-call-recently-updated-articles.html

请按「Ctrl+D」收藏荒岛分享给好友,如您发现本文教程或宝藏链接已失效,请联系站长修正!

荒岛公众号
扫一扫关注
weinxin
HDsoft27
公众号ID已复制,前往微信关注...
荒岛哔哩姬
扫一扫关注
weinxin
ximagine
哔哩姬ID已复制,前往B站关注...
荒岛广告位招商进行中...
ximagine
荒岛广告位招商进行中...

发表评论