WordPressの記事の公開日と更新日の表示を簡単に切り替え
現状では記事を更新しても公開日のままになってしまいます。
記事を手直ししていく中で、アップデートしたと分かるようにしたいです。
- 記事を始めて公開するときは「公開」を表示
- 修正して更新ボタンを押下したら、「更新」に切り替え
以上の条件でコーディングしました。
<?php if(get_the_time('Y/m/d') != get_the_modified_date('Y/m/d')): ?>
<time class="blog-note__update-day" datetime="<?php the_modified_date("Y-m-d H:i:s") ?>" itemprop="modified">
<i class="fas fa-edit"></i> <?php the_modified_date('Y/m/d'); ?>更新
</time>
<?php else: ?>
<time datetime="<?php the_time('Y-m-d'); ?>">
<i class="fas fa-calendar-alt"></i> <?php the_time('Y年m月d日'); ?>公開
</time>
<?php endif; ?>
表示させたい箇所に埋め込んでみてください。
Fontawesomeは、wordpress/wp-content/themes/mytemplate/functions.phpから読み込んでいるので、<i class="fas fa(任意のアイコンのクラス名)"></i>
と書けばすぐに反映されます。
functions.php
/**
* CSS,JSを読み込ませる
*/
function mytemplate_enqueue_scripts() {
…(省略)
// Enqueue styles
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css');
…(省略)
}
add_action('wp_enqueue_scripts', 'mytemplate_enqueue_scripts');