Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

カテゴリー:WordPressでブログ投稿のタイトルを自動的に切り捨てる方法

私たちが初めてコンテンツ量の多いWordPressサイトを立ち上げたとき、ブログ投稿のタイトルが問題になるとは思ってもみませんでした。長いタイトルの中には、美しいレイアウトを崩してしまうものもありましたし、モバイル端末では乱雑に見えてしまうものもありました。

複数のWordPressサイトを管理し、サイトオーナーを長年にわたって支援してきた結果、投稿タイトルを削ることで、一貫性のある洗練された外観を確保し、オーディエンスの注目を集めることができることがわかりました。

このガイドでは、WordPressブログのタイトルをPHPで自動的に短くする効果的なテクニックをご紹介します。

How to Automatically Truncate Blog Post Titles in WordPress

なぜWordPressでPHPを使って投稿タイトルを切り捨てるのか?

WordPressの投稿タイトルをPHPで切り詰めると、投稿タイトルの長さとサイトでの表示方法をよりコントロールできるようになります。

例えば、WordPressブログのデザインを崩さないように、ホームページ上の長い投稿タイトルをカットしたいかもしれない。

Truncate posts example

そうすることで、ブログを視覚的に美しく保ち、読者がナビゲートしやすくなります。

注:タイトルを短くすることでブログ投稿をSEOに最適化することを目的としている場合、タイトルの切り捨ては必要ないかもしれません。その代わりに、WordPressのSEOプラグインを使えば、短くて効果的なSEOタイトルタグを簡単に作成することができます。

All In One SEO(AIOSEO)をお勧めします。なぜなら、検索結果ページ用のカスタムSEOタイトルを作成できる一方で、投稿日::の長い投稿タイトルを維持できるからです。

詳しくは、AIOSEOの正しい設定方法についての究極のガイドをご覧ください。

それでは、PHPを使ってWordPressの投稿タイトルを切り捨てる2つの方法を紹介しよう:

方法1:WordPressの投稿タイトルをWordPressの関数で切り捨てる

WordPressで投稿タイトルを切り詰める最も簡単な方法は、WordPressファイルにPHPコードを追加することです。WordPressでコードをコピー&ペーストする方法をご覧ください。

多くのチュートリアルでは、テーマのfunctions.phpファイルに直接コードを追加するように説明しています。しかし、間違いがあるとエラーになったり、サイトが壊れたりする可能性があります。

そのため、代わりに無料のWPCodeプラグインを使用することをお勧めします。テーマファイルを直接変更することなく、WordPressサイトのカスタムコードを簡単に追加・管理できる便利なツールです。

WPBeginnerでは、カスタムコードスニペットの追加と管理にプラグインを利用しています。さらに詳しく知りたい方は、WPCodeのレビューをご覧ください。

まず、WPCode無料プラグインをインストールし、有効化する必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。

有効化したら、管理サイドバーからCode Snippets ” + Add Snippetを選択します。次に、「カスタムコードを追加(新規スニペット)」オプションにマウスオーバーし、表示された「カスタム・スニペットを追加」ボタンをクリックします。

WPCode add custom code

新しいページが開きますので、そこにスニペットのタイトルを入力し、コードを追加してください。

以下のコードをWPCodeのコード・プレビュー・ペインにコピー&ペーストするだけです。

1
2
3
4
5
6
7
8
9
10
function max_title_length( $title ) {
$max = 35;
if( strlen( $title ) > $max ) {
return substr( $title, 0, $max ). " …";
} else {
return $title;
}
}
 
add_filter( 'the_title', 'max_title_length');

コードを追加するとこのようになります。

Truncating Headlines Using WPCode

このコードはWordPressの投稿ループ内で実行され、ブログ投稿のタイトルを35文字に短縮します。タイトルの長さを変更するには、$max変数にお好みのタイトルの長さを設定するだけです。

お困りの場合は、WordPressでカスタムコードを追加する方法の投稿をご覧ください。

次に、コードタイプのドロップダウン・メニューから「PHP Snippet」を選択する必要がある。

最後に、有効化設定をオンにして、「スニペットを保存」ボタンをクリックしてスニペットを保存します。

Truncating Headlines Using WPCode

コードスニペットが有効化されると、WordPressサイト上のどこに表示されても、ブログ投稿のタイトルが短縮されます。

下のスクリーンショットのように、タイトルは35文字で止まり、最後に省略記号「…」が続く。

WPCode truncation example

方法2:WordPressのテーマファイルを変更して、PHPでWordPressの投稿タイトルを切り詰める

WordPressの投稿タイトルを切り詰めるもう一つの方法は、WordPressのテーマファイルに直接コードを追加することです。

この方法では、タイトルを短くする場所をよりコントロールすることができます。例えば、ホームページ上ではタイトルをカットし、ブログ投稿日ではタイトルを表示したい場合などです。

⚠️Pro Tip:方法2を続ける前に、テーマを更新するとカスタムの変更が上書きされる可能性があることを覚えておいてください。

カスタマイズを失うことなくサイトを最新の状態に保つには、カスタマイズを失うことなくWordPressテーマを更新する方法についてのガイドをご覧ください。

そのためには、ブログ投稿のタイトルを切り捨てたいWordPressテーマファイルに直接PHPコードを追加する必要があります。

例えば、以下のコードスニペットをindex.phpファイルに追加し、WordPress投稿ループ内の既存のthe_titleタグを置き換えることで、サイト全体のタイトルの長さを変更することができます:

1
2
3
4
5
6
7
8
9
<a href="<?php the_permalink() ?>">
<?php
$thetitle = $post->post_title; /* or you can use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 25;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>
</a>

このコードは投稿タイトルの長さを25文字にセットします。もしタイトルの長さが25文字より長い場合は、25文字の後にタイトルをカットし、最後に省略記号’…’を追加します。

サイトの文字数を変更するには、$thelength変数をお好みの文字数に変更するだけです。

コードを追加してファイルを保存したら、WordPressホスティングアカウントのテーマディレクトリにアップロードする必要があります。

FTPクライアントまたはWordPressホスティングコントロールパネルのファイルマネージャツールを使用してこれを行うことができます。FTPを使用したことがない場合は、FTPを使用してWordPressにファイルをアップロードする方法のガイドをご覧ください。

コードを追加すると、投稿タイトルは設定した文字数で切り捨てられます。

PHPでWordPressの投稿タイトルを切り捨てる方法について、この記事がお役に立てば幸いです。また、優れたブログ記事の書き方に関するガイドや、WordPressのエキスパートが選ぶ最高のコンテンツマーケティングツールとプラグインもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

20件のコメントLeave a Reply

  1. dustinporchia

    This is golden!….Thanks wpbeginner!

  2. Junaid

    Sweet! was just looking for a clients project

  3. Marco

    Does anybody know how the link title of previous_post_link(); could be truncated?

    thanks

  4. Brow

    Thanks this worked perfectly! I didn’t want to end up using a plugin just to do this and was happy your code cut down the titles properly.

    Thanks again!

  5. Joey Figaro

    Hey there – thanks for writing this up! I happened to stumble upon another example of how to achieve this and it seemed a lot more simple, so I will share it with you and see what you think.

    functions.php:

    function new_excerpt_length($length) {
    return 100;
    }
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

    • Editorial Staff

      These are two entirely different concepts…. The one you recommend is for post excerpts whereas the one we are talking about is for Post Titles.

      Admin

  6. Lena

    Hi!
    This doesn’t work if you use other languages than english. My swedish titel looks awful because the code doesn’t translate å ä and ö comparing to the default code. Any suggestions of what I have to do? This is a good trick and i want to use it.

    Kindly Lillan

    • Editorial Staff

      Hmm… that does sound like a serious issue. Wondering if you can specify the language via PHP, so it counts characters in that instead of english.

      Admin

      • Alex

        You might wanna try to specify a different charset, check Latin1 or utf8 i think they contain those chars as well.. I had some similar issues recently since my first language is german :)

  7. Navjot Singh

    One Suggestion, this type of code should be included in functions.php and not index.php. You can use conditional tags to restrict the code to any page you want whether its the homepage or any other page where you want.

    • Editorial Staff

      This is just for specific areas… But yes, it can be customized and placed in functions.php

      Admin

  8. Thomas Scholz

    Don’t use strlen(). Use mb_strlen() or strlen(utf8_decode($str)) or you risk to truncate the string inside of a multi-byte character. The same applies to mb_substr().

    Oh, and an ellipsis is one character: … :)

    • snipsley

      Thanks!! mb_strlen() a mb_substr solved my encoding problem. I’ve been looking for this for hours!

    • Lena Backstedt

      BIG tnx!
       
      mb_strlen() also seems to work for the swedish language (so far I can see)

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.