Skip to content

Instantly share code, notes, and snippets.

@kurudrive
Last active May 31, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurudrive/ffac5381f0d035a8027bdfdf97938607 to your computer and use it in GitHub Desktop.
Save kurudrive/ffac5381f0d035a8027bdfdf97938607 to your computer and use it in GitHub Desktop.
WordPressで 記事タイトルに NEW 表示を追加
function my_add_new_before_post_title( $title ) {
// 今日の日付
$today = date_i18n( 'U' );
// 投稿の日付
$entry_date = get_the_time( 'U' );
// 経過日数
$past_day = date( 'U', ( $today - $entry_date ) ) / 86400;
// Newを表示する日数
$display_day = 7;
if ( $display_day >= $past_day && get_post_type() == 'post' ) {
// 記事タイトルの前に NEWを追加
$title = '<span class="icon-new">NEW</span>' . $title;
// 出力側で esc_html( get_the_title() ) などのような書き方をされていると
// 残念ながらHTMLは使えません。その場合は諦めて直接テンプレートを改変してください。
}
return $title;
}
add_filter( 'the_title', 'my_add_new_before_post_title' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment