本色能是展现站内历史上的今天宣布的文章,而不是以前历史上发生的小事。以是想失常应用本色能,首先需要的是你站点运行高出1年了,如果还不到一年,那自然不会有甚么历史上的今天了。
今天我就给人人带来怎样无需插件应用代码来实现历史上的今天这共性能,本色能是应用wordpress自带的date_query来实现。
下面的代码退出到functions.php中
function fa_today_in_histroy(){$today = getdate();$args = array('date_query' => array(array('year'=> $today['year'],'compare' => '!=',),array('month' => $today['mon'],'day' => $today['mday'],),),);$postlist = get_posts($args);$html = '历史上的今天
';if(!empty($postlist)){foreach($postlistas$key=>$post){$html.='- ID).'"title="'.$post->post_title.'">'.$post->post_title.'
';}$html.='
';return$html;}}
细致,在这个循环中是不能间接调用各种wp文章函数的,如需应用wp文章函数,则需要应用setup_postdata()
以及wp_reset_postdata()
如果是增添到文章末端,可间接应用下列钩子,间接增添到functions.php
中就可
function add_today_in_histroy($content){global $post;return $content . today_in_histroy();}add_filter('the_content','add_today_in_histroy');
如果是自界说地位,则应用
本文链接:https://addon.ciliseo.com/wordpress-dai-ma-shi-xian-li-shi-shang-de-jin-tian-gong-neng.html
网友评论