✨ 楽天市場 売れ筋ランキング ✨

ワードプレス記事タイトル/本文を半角0.5文字換算して文字数を記事一覧に表示させる方法

ワードプレス記事タイトル/本文を半角0.5文字計算にして文字数を記事一覧に表示させる方法 ワードプレス

X(旧Twitter)などのSNSに自動ポストする際に文字数が制限を超えないようにしなければいけません。
タイトルや本文の文字数が幾つなのか?を記事一覧に表示する方法を紹介します。

 

テーマによって文字数が表示されるが・・

ワードプレスでは使用するテーマによっては初めからタイトルや本文の文字数が記事一覧に表示されるものもあります。

例えば有名テーマのCocoonはタイトルと本文の文字数が記事一覧に表示されます。

 

ただ、この文字数というのは全角も半角も1文字で計算されているのでXなどに投稿する時に、投稿可能な正確な文字数が分からないんですよね。(X(旧 Twitter)の文字数は最大で全角140字、半角280字)

その為に半角文字を0.5文字で計算する必要があります。

これから説明する方法を使用すれば半角文字を0.5文字で計算してくれるのでとても便利だと思います。

 

半角文字を0.5文字で計算し文字数を記事一覧に表示する手順

  • タイトルと本文の文字数をそれぞれ表示
  • タイトルと本文の合計文字数を表示
  • タイトルのみの文字数を表示

の順に紹介していきます。

以下のコードを使用しているテーマのfunction.phpに貼り付けて下さい。(子テーマを使用している場合は子テーマへ)

function.phpを編集する前に必ずバックアップを取って下さい。function.phpは間違うとエラーでサイトが正常に表示されなくなってしまいます。

タイトルと本文の文字数をそれぞれ表示

/***********************************
記事一覧にタイトルと本文文字数を表示
***********************************/

// 投稿一覧にカラムを追加(タイトル文字数・本文文字数)
function add_custom_length_columns($columns) {
$columns['title-length'] = 'タイトル文字数';
$columns['content-length'] = '本文文字数';
return $columns;
}
add_filter('manage_post_posts_columns', 'add_custom_length_columns');

// カスタムカラムに文字数を表示(半角0.5、全角1)
function show_custom_length_in_column($column_name, $post_id) {
$max_title_length = 40;
$max_content_length = 1000;

if ($column_name == 'title-length' || $column_name == 'content-length') {
if ($column_name == 'title-length') {
$text = get_the_title($post_id);
$max_length = $max_title_length;
} else {
$post = get_post($post_id);
$text = $post->post_content;
$text = wp_strip_all_tags($text); // HTMLタグ除去
$max_length = $max_content_length;
}

$text = preg_replace('/\s+/', ' ', $text); // 改行・空白調整
$length = custom_text_length($text); // カスタム文字数カウント

$text_class = '';
if ($length > $max_length) {
$text_class = ' style="color:red; font-weight:bold;"';
}

echo '<span class="word-count"' . $text_class . '>' . round($length, 1) . '</span>';
}
}
add_action('manage_posts_custom_column', 'show_custom_length_in_column', 10, 2);

// 半角0.5・全角1でカウントする共通関数
function custom_text_length($text) {
$length = 0;
for ($i = 0; $i < mb_strlen($text); $i++) {
$char = mb_substr($text, $i, 1);
if (mb_strwidth($char) == 1) {
$length += 0.5;
} else {
$length += 1;
}
}
return $length;
}

// 管理画面スタイルのカスタマイズ
function custom_admin_styles() {
echo '<style>
/* カラム幅とスタイル調整 */
.wp-list-table th.column-title-length,
.wp-list-table td.column-title-length,
.wp-list-table th.column-content-length,
.wp-list-table td.column-content-length {
width: 130px !important;
text-align: center !important;
padding-left: 5px !important;
padding-right: 5px !important;
}

.wp-list-table th, .wp-list-table td {
padding: 5px !important;
}

.wp-list-table {
margin-right: 0 !important;
}

.wp-list-table th {
background-color: #f9f9f9 !important;
}
</style>';
}
add_action('admin_head', 'custom_admin_styles');

指定した文字数をオーバーすると赤く表示される

以下の箇所を変更する事で、任意に指定した文字数をオーバーすると赤く表示されます。

$max_title_length = 40;
$max_content_length = 1000;
指定文字数を超えるとこのようになる(タイトル40文字、本文1,000文字で指定)

 

タイトルと本文の合計文字数を表示

/*****************************************
記事一覧にタイトルと本文文字数の合計を表示
******************************************/

// 投稿一覧に「タイトルと本文の文字数」カラムを追加
function add_title_and_content_length_column($columns) {
$columns['title-content-length'] = 'タイトルと本文文字数';
return $columns;
}
add_filter('manage_post_posts_columns', 'add_title_and_content_length_column');

// タイトルと本文の文字数をカウントして表示(半角0.5、全角1として計算)
function show_title_and_content_word_count_in_column($column_name, $post_id) {
if ($column_name == 'title-content-length') {
// タイトルの文字数を取得
$title = get_the_title($post_id);
// HTMLタグを削除
$title = strip_tags($title);
// 改行コードや余分な空白を削除
$title = preg_replace('/\s+/', ' ', $title);
// タイトルの文字数
$title_length = custom_text_length($title);

// 本文の文字数を取得
$content = get_post_field('post_content', $post_id);
// HTMLタグを削除
$content = strip_tags($content);
// 改行コードや余分な空白を削除
$content = preg_replace('/\s+/', ' ', $content);
// 本文の文字数
$content_length = custom_text_length($content);

// タイトルと本文の合計文字数
$total_length = $title_length + $content_length;

$max_length = 2000; // 最大文字数

// 文字数制限を超えた場合は赤色で強調表示
$text_class = '';
if ($total_length > $max_length) {
$text_class = ' style="color:red; font-weight:bold;"';
}

echo '<span class="word-count-title-content-only"' . $text_class . '>' . round($total_length, 1) . '</span>';
}
}
add_action('manage_posts_custom_column', 'show_title_and_content_word_count_in_column', 10, 2);

// 半角0.5・全角1でカウントする共通関数
function custom_text_length($text) {
$length = 0;
for ($i = 0; $i < mb_strlen($text); $i++) {
$char = mb_substr($text, $i, 1);
// 半角文字を0.5、全角文字を1としてカウント
if (mb_strwidth($char) == 1) {
$length += 0.5; // 半角文字は0.5
} else {
$length += 1; // 全角文字は1
}
}
return $length;
}

// 管理画面のカスタムスタイル
function custom_admin_styles() {
echo '<style>
/* "タイトルと本文文字数" カラムの幅を調整 */
.wp-list-table th.column-title-content-length,
.wp-list-table td.column-title-content-length {
width: 120px !important; /* カラム幅を調整、必要に応じて変更 */
text-align: center !important; /* 中央に配置 */
padding-left: 5px !important;
padding-right: 5px !important;
}

/* 投稿一覧ページ全体の余白を調整 */
.wp-list-table th, .wp-list-table td {
padding: 5px !important; /* 全体の余白を狭める */
}

/* 投稿一覧ページの右側の余白を調整 */
.wp-list-table {
margin-right: 0 !important; /* 右のマージンをリセット */
}

/* カラムヘッダーの色調整 */
.wp-list-table th {
background-color: #f9f9f9 !important;
}
</style>';
}
add_action('admin_head', 'custom_admin_styles');

指定した文字数をオーバーすると赤く表示される

以下の箇所を変更する事で、任意に指定した文字数をオーバーすると赤く表示されます。

$max_length = 2000; // 最大文字数
指定文字数を超えるとこのようになる(タイトル+本文合計2,000文字で指定)

 

タイトルのみの文字数を表示

/*****************************
記事一覧にタイトル文字数を表示
******************************/

// 投稿一覧に「タイトル文字数」カラムを追加
function add_title_length_column($columns) {
$columns['title-length'] = 'タイトル文字数';
return $columns;
}
add_filter('manage_post_posts_columns', 'add_title_length_column');

// タイトルの文字数をカウントして表示(半角0.5、全角1として計算)
function show_title_word_count_in_column($column_name, $post_id) {
if ($column_name == 'title-length') {
// タイトルの文字数を取得
$title = get_the_title($post_id);
// HTMLタグを削除
$title = strip_tags($title);
// 改行コードや余分な空白を削除
$title = preg_replace('/\s+/', ' ', $title);
// タイトルの文字数
$title_length = custom_text_length($title);

$max_length = 40; // 最大文字数

// 文字数制限を超えた場合は赤色で強調表示
$text_class = '';
if ($title_length > $max_length) {
$text_class = ' style="color:red; font-weight:bold;"';
}

echo '<span class="word-count-title-only"' . $text_class . '>' . round($title_length, 1) . '</span>';
}
}
add_action('manage_posts_custom_column', 'show_title_word_count_in_column', 10, 2);

// 半角0.5・全角1でカウントする共通関数
function custom_text_length($text) {
$length = 0;
for ($i = 0; $i < mb_strlen($text); $i++) {
$char = mb_substr($text, $i, 1);
// 半角文字を0.5、全角文字を1としてカウント
if (mb_strwidth($char) == 1) {
$length += 0.5; // 半角文字は0.5
} else {
$length += 1; // 全角文字は1
}
}
return $length;
}

// 管理画面のカスタムスタイル
function custom_admin_styles() {
echo '<style>
/* "タイトル文字数" カラムの幅を調整 */
.wp-list-table th.column-title-length,
.wp-list-table td.column-title-length {
width: 120px !important; /* カラム幅を調整、必要に応じて変更 */
text-align: center !important; /* 中央に配置 */
padding-left: 5px !important;
padding-right: 5px !important;
}

/* 投稿一覧ページ全体の余白を調整 */
.wp-list-table th, .wp-list-table td {
padding: 5px !important; /* 全体の余白を狭める */
}

/* 投稿一覧ページの右側の余白を調整 */
.wp-list-table {
margin-right: 0 !important; /* 右のマージンをリセット */
}

/* カラムヘッダーの色調整 */
.wp-list-table th {
background-color: #f9f9f9 !important;
}
</style>';
}
add_action('admin_head', 'custom_admin_styles');

指定した文字数をオーバーすると赤く表示される

以下の箇所を変更する事で、任意に指定した文字数をオーバーすると赤く表示されます。

$max_length = 40; // 最大文字数
指定文字数を超えるとこのようになる(40文字で指定)

 

本文の文字数を表示させると読み込みが遅くなる

実際にやってみると分かりますが、タイトルの文字数のみの表示であれば問題ありませんが、本文文字数も表示させると文字数の取得に時間が掛かります。

本文の文字数が多いサイトだと文字数に比例して時間が掛かる筈です。

使い方次第でしょうが、SNSへの自動投稿(ポスト)に使うのであれば「記事タイトル+ページURL」というパターンが多いと思うので、その場合はタイトルの文字数だけ把握できれば問題ないでしょう。

参考までにXにポストする際に自サイトへのリンク(URL)を貼る場合は、どれだけURLが長くても23文字までしかカウントされません。(23文字以下ならその文字数が適用)

他にも色々使い方があると思うので是非この方法を活用して下さい(^_-)-☆

タイトルとURLをコピーしました