wordpress 公開記事にnewマークを付ける

Filed under: php,wordpress — kdcs @ 16年11月26日 土曜日

公開日時を基準とした場合
「date_i18n」はwordpressで日本時間を取得する関数

<?php
  $days = 3;
  $today = date_i18n('U');
  $entry = get_the_time('U');
  $elapsed = date('U',($today - $entry)) / 86400;
    if( $days > $elapsed ){
       echo '<span class="new">New</span>';
  }
?>

公開日時に関係なく新しいのもからn件といった場合

<?php
$my_query = new WP_Query(array(
'cat' => 1, //カテゴリーID
'posts_per_page' => 10, //表示件数
'orderby' => 'date',
'order' => 'DESC'
));
?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<b><?php the_time("Y.n.j"); ?></b> <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php
//New!を付ける件数
if ($my_query->current_post < 3) {
echo 'New!';
}
?>
<br />
<?php endwhile; ?>

サイト内検索

カテゴリー

最近の投稿

« |wordpress 公開記事にnewマークを付ける| »
↑上に戻る