WordPressプラグイン『what’s new generator』でカテゴリ名を表示する方法。
① 投稿情報からカテゴリ名を取得し、変数に格納する。
※以下実装は、先頭カテゴリ1件のみ画面表示する。
複数表示する場合は、もう少し手を加える。
whats-new-generator.php
public function __construct( $post ){
$options = WNG::get_option();
$orderby = $options['wng_orderby'];
$this->raw_date = $orderby == '公開日順' ? $post->post_date : $post- post_modified;
$this->date = date(get_option('date_format'), strtotime($this->raw_date));
$this->title = esc_html( $post->post_title );
$this->url = get_permalink($post->ID);
$this->newmark = $this->is_new();
↓↓↓追加
$this->category = get_the_category( $post->ID )[0]->name;
self::$number++;
}
② ①で格納した変数名を指定し、画面表示する。
CSSもカスタマイズ可能(whats-new.css)
whatsnew-view.php
<dd>
<?php if ( $item->newmark ): ?>
<span class='newmark'>NEW!</span>
<?php endif; ?>
↓↓↓追加
<span class='newmark'><?php echo $item->category; ?></span>
<?php echo $item->title; ?>
</dd>
【画面結果】
”業務案内”が表示された。
コメント