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

カテゴリー:WordPressで投稿タイトルから外部リンクにリンクする方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressでページや投稿のタイトルを外部リンクにしたいですか?

投稿日: ユーザーが投稿タイトルをクリックしたときに、WordPressサイトから外部のサイトやページにリダイレクトさせたい場合があります。これは、あなたのサイトをハブとして他のコンテンツにトラフィックを誘導する場合に便利です。

WordPressは初期設定ではこれを許可していませんが、WPBeginnerの読者から解決策を求める声がありましたので、いくつかご紹介します。

今回は、WordPressで投稿やページのタイトルから外部リンクにリンクする方法を紹介する。

How to Link to External Links from the Post Title in WordPress

WordPressの投稿タイトルにリンクを追加するタイミングは?

WordPressの投稿、ページ、カスタム投稿タイプのタイトルをカスタムURLにリンクすると便利な場面がいくつかあります。

WordPressサイトで通常の投稿に似た広告スペースを提供するとしよう。タイトルはあなたのコンテンツではなく、広告主のサイトにリンクさせることができ、ユーザーにとって明確な導線を作ることができます。

あるいは、自社コンテンツと他ソースからのキュレーション記事の両方を掲載するニュースアグリゲーターサイトを運営しているかもしれない。理想的なのは、ホームページのニュースセクションが、投稿記事のタイトルを、あなたのサイトであろうと外部のサイトであろうと、適切なリンク先に自動的にリンクしてくれることだ。

あるいは、ホームページで商品を紹介しているにもかかわらず、その商品をクリックすると個別の商品ページが表示される場合があります。タイトルを直接カテゴリの製品カタログページにリンクさせ、より広い概要を示す方がいいかもしれません。

すべてを念頭に置いて、WordPressサイトの投稿タイトルを外部URLにリンクする方法を説明しましょう。チュートリアルのナビゲーションには、以下のクイックリンクをご利用ください:

この方法は初心者や、投稿やページのタイトルを外部URLにリンクさせるシンプルなソリューションを求める人に最適です。

さらに、タイトルをサイト全体のカスタマイザーURLにリンクさせたい場合にも、この方法は最適な設定です。

まず、Page Links Toプラグインをインストールする必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。

プラグインをインストールして有効化したら、新しい投稿、ページ、カスタム投稿タイプを開くか、ブロックエディターを使って既存の投稿を編集してください。投稿設定のサイドバーに’Page Links To’という新しいタブが表示されます。

ここで、2つのオプションが表示されます:通常のWordPressのURL」(初期設定)と「カスタマイザーURL」の2つのオプションがあります。

Using the Page Links To plugin in the block editor

次に、’Links To’フィールドに完全な外部URL(https:// を含む)を貼り付けるだけです。例えば、WPFormsのサイトにリンクする場合、’https://wpforms.com’と入力します。

外部サイトへの訪問者がタイトルをクリックしたときに、元のタブであなたのページを開いたままにしたい場合は、「新しいタブで開く」のチェックボックスもあります。必要であれば、このボックスにチェックを入れてください。

最後に、「更新」または「公開」をクリックして変更を保存します。

WordPressのブログページ、ホームページ、アーカイブページなど、投稿やページのタイトルが抜粋として表示されている場所に行ったら、タイトルにマウスオーバーしてみてください。

指定した外部URLにリンクしていることがわかるだろう。

Successfully adding an external link to a post title using the Page Links To plugin

方法2:カスタムコードを使う(よりコントロールしやすくなる)

この方法は、コードに慣れているユーザーや、投稿タイトルが別のURLにリンクするページをよりコントロールしたい人に最適です。

例えば、個別投稿テンプレートで表示される場合は投稿タイトルを外部URLにリンクさせたいが、ホームページやアーカイブページで表示される場合はリンクさせたくない場合などです。

物事を安全にするために、テーマファイルを直接編集する代わりにWPCodeを使用します。このプラグインは、WordPressにカスタムコードを挿入する際に、コードにエラーがあった場合にサイトが壊れるのを防ぐことができるため、安全です。

さらに詳しく知りたい方は、WPCodeプラグインのレビューをご覧ください。

ステップ1:WPCodeのインストールとセットアップ

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

有効化したら、Code Snippets+ Add Snippetに移動します。ここで、’カスタムコードを追加する(新規スニペット)’を選択し、’スニペットを使用する’ボタンをクリックします。

Adding custom code in WPCode

次に、「コードタイプ」を「HTMLスニペット」から「PHPスニペット」に変更する。

スニペットには「投稿日からの外部リンク」のようなわかりやすい名前をつけ、後で簡単に識別できるようにしましょう。

Creating a new WPCode code snippet for linking to external links from post titles

では、次のコード・スニペットのいずれかを選んでください:

設定1:すべての投稿タイトルを外部URLにリンクさせる

このコードスニペットは、個別投稿、ホームページ、アーカイブページなど、サイトの様々な場所で投稿タイトルのURLを変更します。

コードは個別投稿ページ、ホームページ、フロントページ、アーカイブページかどうかをチェックします。もしそうで、後述の手順でカスタマイザーURLを設定した場合、コードは初期設定のパーマリンクの代わりにそのURLを使用します。

次のステップでブロックエディターに’custom_url’というカスタムフィールドを追加し、各投稿の外部URLを指定します。

// Change post title URL for single post pages and homepage/front page
add_filter('post_link', 'check_for_custom_url', 10, 3);
function check_for_custom_url($permalink, $post, $leavename) {
$custom = false;
// Check if it's a single post page
if (is_single()) {
$custom = get_post_meta($post->ID, 'custom_url', true);
}
// Check if it's the homepage, front page, or archive pages
elseif (is_home() || is_front_page() || is_category() || is_tag() || is_archive()) {
$custom = get_post_meta($post->ID, 'custom_url', true);
}
// Return the custom URL if set, otherwise return the default permalink
return ($custom) ? esc_url($custom) : $permalink;
}

例えば、このコードをフロントページでのみ実行させ、アーカイブページでは実行させないとしよう。

そうすれば、is_category()is_tag()is_archive()といったコードを削除すればよい:

// Change post title URL for single post pages and homepage/front page
add_filter('post_link', 'check_for_custom_url', 10, 3);
function check_for_custom_url($permalink, $post, $leavename) {
$custom = false;
// Check if it's a single post page
if (is_single()) {
$custom = get_post_meta($post->ID, 'custom_url', true);
}
// Check if it's the homepage, front page, or archive pages
elseif (is_home() || is_front_page()) {
$custom = get_post_meta($post->ID, 'custom_url', true);
}
// Return the custom URL if set, otherwise return the default permalink
return ($custom) ? esc_url($custom) : $permalink;
}

設定2:個別投稿テンプレートの投稿タイトルを外部URLにリンクさせる

このコードは、個別投稿テンプレートの投稿タイトルだけを外部URLにリンクさせ、ホームページやアーカイブページなどの他のページにはリンクさせたくない場合に便利です。

こうすることで、ユーザーはブログ記事を訪問して読むことはできるが、投稿日: をクリックして別のリソースをチェックすることもできる。

例えば、ポッドキャストサイトがそうだ。あなたのサイトを使ってエピソードの要約やトランスクリプトを公開し、音声ファイルはSpotifyでホスティングすることができます。この方法では、ポッドキャストのエピソードをサイトで公開しながら、投稿タイトルをクリックするとSpotifyに誘導することができます。

以下のコードは、現在のリクエストが個別投稿かどうかをチェックします。もしそうで、カスタムフィールドを使ってカスタムURLを設定した場合、投稿タイトルにそのURLが使われます:

// Hook into the 'post_link' filter to modify the permalink of a post.
add_filter('post_link', 'check_for_custom_url', 10, 3);
// Define the callback function for the 'post_link' filter.
function check_for_custom_url($permalink, $post, $leavename) {
// Check if the current request is for a single post.
if (is_single()) {
// Retrieve the value of the 'custom_url' custom field for the current post.
$custom = get_post_meta($post->ID, 'custom_url', true);
// If the custom field has a value, use it as the permalink. Otherwise, use the default permalink.
return ($custom) ? esc_url($custom) : $permalink;
} else {
// If the current request is not for a single post, return the default permalink.
return $permalink;
}
}

コード・スニペットを貼り付けたら、「インサーター」セクションまでスクロールダウンする。Insert Method」が「Auto Insert」になっており、「Location」が「Run Everywhere」になっていることを確認してください。

最後に、トグルを使ってスニペットを有効化し、「スニペットを保存」をクリックする。

Choosing the insertion method and location in WPCode

ステップ2:タイトルへのリンクを有効化する(ブロックテーマユーザーのみ)

ブロックテーマを使用している場合、コードが動作することを確認するために行うべき追加のステップがあります。

まず、外観エディターで フルサイトエディターを開きます。

Selecting the Full-Site Editor from the WordPress admin panel

ブロックテーマを編集するオプションが表示されます。

先に進み、『テンプレート』をクリックする。

Opening the Templates menu in full-site editor

この段階で、「個別投稿」テンプレートを探す。

見つけたらクリックしてください。

Opening the Single Posts template in the full-site editor

ここで、鉛筆の「編集」アイコンをクリックする。

これでブロックエディターが開きます。

Editing the Single Posts template in the full-site editor

エディター内に入ったら、「タイトル」ブロックをクリックする。

ブロック設定のサイドバーで、「タイトルをリンクにする」オプションと、オプションの「新しいタブで開く」オプションを有効化します。Link Rel’フィールドは空のままにしておきます。

その後、「保存」をクリックする。

Enabling linking in post titles in the block editor

ステップ 3: 新規カスタムフィールドの追加

では、ブロックエディターに’custom_url’カスタムフィールドを追加してみましょう。

エディター内で、ブロックエディターの右上にある3つの点のメニューをクリックし、「環境設定」を選択する。

Opening the Preferences menu in the block editor

一般」タブにナビゲーションしてください。

その後、’カスタムフィールド’までスクロールダウンします。有効化し、’Show & Reload Page’をクリックしてページを更新します。

Enabling custom fields in the block editor

新しいカスタムフィールドセクションが外観されます。

そこで「新規入力」をクリックする必要がある。

Creating a new custom field in the block editor

新規カスタムフィールドの追加’セクションで、’custom_url’または先ほどのコードで指定したカスタムフィールドをラベルフィールドに入力します。

値」フィールドに、リンクしたい外部URLを貼り付けます。完了したら、「カスタムフィールドを追加」をクリックするだけです。

Adding a new custom field in the block editor

次に、’更新’または’公開’をクリックするだけで、変更が反映されます。

最後に、サイトにアクセスしてコードが機能しているかどうかを確認する。投稿タイトルの上にマウスオーバーするか、Inspectツールを使って確認できます。

オプション2のコードを使用した場合、投稿タイトルのURLは以下のようになります:

An example of an external link successfully added to post titles with WPCode

カテゴリー: 投稿タイトルに外部リンクを追加することはSEOに影響するか?

投稿タイトルに直接外部リンクを追加しても、検索エンジン最適化(SEO)への影響は最小限です。

しかし、考慮すべきことがいくつかある。他のサイトにリンクを張るということは、基本的に検索エンジンに他のサイトが良い情報源である可能性を伝えるということです。あなたの “リンクジュース “の一部は外部サイトに渡されるかもしれませんが、それは一般的に少量です。

とはいえ、タイトルに外部リンクが多く含まれていると、ユーザーを混乱させたり、すぐにサイトを離れてしまうのではないかと思わせたりする可能性があります。

それでもタイトルに外部リンクを使いたい場合は、その外部サイトが本当にコンテンツに付加価値を与え、高品質でオーディエンスとの関連性が高い場合にのみリンクを張ることをお勧めします。

また、タイトルコードの外部リンクに「nofollow」属性を追加することもできます。これにより、検索エンジンはランキングのためにリンクをたどらないようになります。

一方、古いサイトから新しいサイトへユーザーを誘導するためにこのチュートリアルを読んでいるのであれば、代わりにリダイレクトの使用を検討した方がよいでしょう。

リダイレクトは、検索エンジン(およびユーザー)に対して、特定のページが永久的に新しい場所に移動したことを伝えます。元のページからのリンクジュースが新しい場所に移動するため、WordPressのSEOにとって有益です。

All In One SEO (AIOSEO)はWordPressプラグインです。ユーザーフレンドリーなインターフェースで、特定の投稿やページのリダイレクトを簡単に設定できます。コードは必要なく、初心者に優しいプラグインです。

All in One SEO Redirects Tab

このトピックについては、301リダイレクトの設定に関する初心者向けガイドでさらに詳しく説明しています。

この投稿が、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.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

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.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

22件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. John says

    Will this work on images relating to the post titles as well? So clicking on either image or post title takes you to the page on the external site?

  3. Alessandro says

    is there a way to link post titles to custom links, but only if they are present in one page (so not for the whole site)?

    Example: search results page of plugins like search & filter or toolset

  4. Kam says

    Thank you. This is really helpful. Would this plugin work for automated RSS feeds? i.e. for aggregator sites? I’m assuming no, as the titles are constantly changing. I’m trying to take the user to the original source with one click on the title.

    • WPBeginner Support says

      You would want to reach out to the support for the aggregator tool you are using for the options you have available.

      管理者

  5. mostafa says

    Thank you for this tutorial. It helped me a lot but how to use this for cpt (in my case a testimonial) and open the link in a new tab . Thank you.

  6. mehmet says

    Thank you for useful information.
    My English is a little bad.
    I want to use this kind of plugin on my site
    But the bold type in the text will be automatically linked to the text.
    Links to other posts within the site
    Is there such an extension?

  7. Tammy says

    Is there a way to require the external url to be unique? I’m using wp-directory and would love to be able to make this custom field “unique required”

  8. Melch Wanga says

    Its a good post indeed. In response to Toni, in my case I am developing a website for film production company and I have “Equipment Hire” as a services in ‘service’ custom post type. Equipment Hire is quite huge and I am using WooCommerce to add the various equipment hence I have Shop page that lists all the equipment available for hire. The trick is to ensure that whenever a user clicks on “Equipment Hire” service, they are directed to the Page set as the Shop page instead of the default Equipment Hire single post page.

  9. Palashtd says

    Recently I have started blogging.
    I could think how to add External Links from the Post Title. But finally I got an awesome tip from this post. I have tested by following this tutorial. I am happy to get these tips because it’s working for me.
    Thanks for sharing this information.

  10. Toni Weidman says

    I’m not sure why you would want to do this. Can you clarify what the purpose of this process is. Thank you.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。