最近在研究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><span class="updatetime"><br />» 修改时间: '
.get_the_modified_time('Y.m.d G:i').'</span></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>None data.</li>' : $recently_updated_posts;
echo $recently_updated_posts;
}
function clear_cache_zww() {
update_option('recently_updated_posts', ''); // 清空 recently_updated_posts
}
add_action('save_post', 'clear_cache_zww'); // 新发表文章/修改文章时触发更新
调用方法:
<h3>Recently Updated Posts</h3>
<ul>
<?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?>
</ul>