【WordPress】understrap テーマ利用時に表示される「投稿日 %s」等の%s記号の非表示方法

スケルトンテーマのunderstrap

WordPressテーマの「understrap」は、Bootstrapを含んだスターターテーマです。

Sassが標準搭載されており、gulpが利用できる環境ではすぐにSassをもちいてデザイン構築ができます。

翻訳箇所に「%s」が表示される問題

カスタマイズ向けのシンプルなデザインのテーマですが、understrapをもちいているサイトを見ると、翻訳箇所と思われる部分に「%s」の表示がされてしまっているケースが多く見られます。

例)

Understrapの「%s」が表示されてしまっている原因

下記ファイルに含まれる投稿日表示部分

テーマフォルダ/inc/tamplate-tags.php
/**
 * Prints HTML with meta information for the current post-date/time and author.
 */
if ( ! function_exists( 'understrap_posted_on' ) ) {
	function understrap_posted_on() {
		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
		if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
			$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s"> (%4$s) </time>';
		}
		$time_string = sprintf( $time_string,
			esc_attr( get_the_date( 'c' ) ),
			esc_html( get_the_date() ),
			esc_attr( get_the_modified_date( 'c' ) ),
			esc_html( get_the_modified_date() )
		);

		$posted_on   = apply_filters(
			'understrap_posted_on', sprintf(
				'<span class="posted-on">%1$s<a href="%2$s" rel="bookmark">%3$s</a></span>',
				esc_html_x( 'Posted on', 'post date', 'understrap' ),
				esc_url( get_permalink() ),
				apply_filters( 'understrap_posted_on_time', $time_string )
			)
		);



		$byline      = apply_filters(
			'understrap_posted_by', sprintf(
				'<span class="byline"> %1$s<span class="author vcard"><a class="url fn n" href="%2$s"> %3$s</a></span></span>',
				$posted_on ? esc_html_x( 'by', 'post author', 'understrap' ) : esc_html_x( 'Posted by', 'post author', 'understrap' ),
				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
				esc_html( get_the_author() )
			)
		);
		echo $posted_on . $byline; // WPCS: XSS OK.

	}
}

上記部分の投稿日、投稿URL表示箇所

sprintf('<span class="posted-on">%1$s<a href="%2$s" rel="bookmark">%3$s</a></span>',esc_html_x( 'Posted on', 'post date', 'understrap' ),esc_url( get_permalink() ),apply_filters( 'understrap_posted_on_time', $time_string ))

さらに絞り込むと

esc_html_x( 'Posted on', 'post date', 'understrap' )

この部分で %s が出力されています。

この 「Posted on」の翻訳の、ソース側に%sがなく、翻訳側に%sが含まれていることが原因になっています。

この翻訳ファイルのソースは、下記テーマ頃のmoファイルになっているようです。

wp-content/languages/themes

Understrapの「%s」を非表示にする方法

テーマフォルダ内のlanguagesフォルダのpoファイルを変更し、moファイルをコンパイルしたものを上書きしましたが、どうやらこのファイルは読み込み対象ではないようです。

故に暫定的ですが、下記場所にあるpoファイルを編集し、moファイルをコンパイルします。

wp-content/languages/themes/understrap-ja.po

poファイルは下記エディタで編集しコンパイルします。

https://poedit.net/

下記編集します。

投稿日:%s
> 投稿日:

編集後、poeditでmoファイルへコンパイルし、下記フォルダに上書きアップロードします。

アップロードファイル:understrap-ja.po、understrap-ja.mo

wp-content/languages/themes

これで%s表記が非表示になりました。

この記事のご感想やコメントはこちら

メールアドレスが公開されることはありません。 が付いている欄は必須項目です