個別記事に投稿者名を公開日の隣に表示する方法【WordPress賢威7】

まるお
こんにちは!まるおです!

今回は記事内に投稿者名を表示させる方法をお伝えします。

 

修正前

 

修正後

 

修正方法

「外観」→「テーマの編集」→「個別投稿(single.php)」を選びます。

 

すると19~22行目に次のようなコードがあるはずです。

<?php if (get_the_time(‘Y-m-d’) != get_the_modified_date(‘Y-m-d’)) { ?>
<p class=”post-date”><?php _e(‘Published on’,’keni’) ?> : <time datetime=”<?php the_time(‘Y-m-d’); ?>” itemprop=”datePublished” content=”<?php the_time(‘Y-m-d’); ?>” ><?php the_time(get_option(‘date_format’)); ?></time> / <?php _e(‘Last modified on’,’keni’) ?> : <time datetime=”<?php the_modified_date(‘Y-m-d’); ?>” itemprop=”dateModified” content=”<?php the_modified_date(‘Y-m-d’); ?>”><?php echo get_the_modified_date(get_option(‘date_format’)); ?></time></p>
<?php } else { ?>
<p class=”post-date”><time datetime=”<?php the_time(‘Y-m-d’); ?>” itemprop=”datePublished” content=”<?php the_time(‘Y-m-d’); ?>” ><?php the_time(get_option(‘date_format’)); ?></time></p>

これが投稿日や更新日を表示するソースになります。

ここに次のように修正して下さい。

 

<?php if (get_the_time(‘Y-m-d’) != get_the_modified_date(‘Y-m-d’)) { ?>
<p class=”post-date”>投稿者 : <?php the_author_nickname(); ?> / <?php _e(‘Published on’,’keni’) ?> : <time datetime=”<?php the_time(‘Y-m-d’); ?>” itemprop=”datePublished” content=”<?php the_time(‘Y-m-d’); ?>” ><?php the_time(get_option(‘date_format’)); ?></time> / <?php _e(‘Last modified on’,’keni’) ?> : <time datetime=”<?php the_modified_date(‘Y-m-d’); ?>” itemprop=”dateModified” content=”<?php the_modified_date(‘Y-m-d’); ?>”><?php echo get_the_modified_date(get_option(‘date_format’)); ?></time></p>
<?php } else { ?>
<p class=”post-date”>投稿者 : <?php the_author_nickname(); ?> / <time datetime=”<?php the_time(‘Y-m-d’); ?>” itemprop=”datePublished” content=”<?php the_time(‘Y-m-d’); ?>” ><?php the_time(get_option(‘date_format’)); ?></time></p>

赤文字部分を追加しただけです。

すると日付の前に投稿者が表示されます!

 

なんで2ヶ所必要なの?

細かい話になりますが、2箇所に追加する必要がある理由を説明します。

上は投稿後に更新がされている場合の表示

下は投稿後に更新がされていない場合の表示

です。

 

投稿日時の枠の外で出す出さないを判断しているので残念ながら2箇所に修正が必要です。

(保守性がちょっと悪いなぁ~)

 

投稿者名(ニックネーム)って何?

「ユーザー」→「あなたのプロフィール」を押してみて下さい。

すると「ユーザー名」「名」「姓」「ニックネーム」「ブログ上の表示名」と入力する箇所があると思います。

ここに設定してある「ニックネーム」が表示されるようになります。

 

ちなみに他の箇所を表示したい場合は少し変数名を変える必要があります。

下記を参考にして書き換えてあげて下さい。

表示の切り替え
  • ユーザー名:the_author_login
  • 名:the_author_firstname
  • 姓:the_author_lastname
  • ニックネーム:the_author_nickname
  • ブログ上の表示名:the_author

 

 

こちらの記事も人気です