functions.php に以下を追加。
$my_posts_hours = 0;
function my_filter_where($where=''){
global $my_posts_hours;
if($my_posts_hours > 0)
$where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-'.$my_posts_hours.($my_posts_hours == 1 ? ' hour' : ' hours'))) . "'";
return $where;
}
function my_posts($hours=0){
global $my_posts_hours;
$my_posts_hours = $hours;
add_filter('posts_where', 'my_filter_where');
$posts = query_posts('showposts=-1');
$count = count($posts);
echo "<p>$hours 時間以内に投稿された記事は $count 件です。</p>";
if(have_posts()) :
?>
<ul>
<?php
while(have_posts()) :
the_post();
?><li><?php the_time('Y.m.d H:i:s'); ?> <a href="<?php the_permalink() ?>"><? the_title() ?></a></li><?php
endwhile;
?>
</ul>
<?php
endif;
remove_filter('posts_where', 'my_filter_where');
wp_reset_query();
}
トップページ用 〜.php の表示させたい箇所に
<?php my_posts(5); ?>
のように書けばOKです(「5」は5時間前の意味)。
記事リンクの出力辺りはお好みにあわせて変更してください。
無事変更することができました。
ありがとうございました。
上記の機能のまま、一部のカテゴリーを除外することはできますか?
例)カテゴリID 123 を除外する場合:
$posts = query_posts('showposts=-1&cat=-123');
参照)
Exclude Categories From Your Home Page