WordPressの投稿コンテンツに広告を挿入したいですか?
投稿コンテンツは、訪問者があなたのサイトに最も関与する場所であり、広告を表示するのに最適な場所です。
この投稿では、WordPressで投稿コンテンツ内に広告を挿入する3つの方法を紹介する。
なぜWordPressで投稿コンテンツ内に広告を挿入するのか?
バナー広告やサイドバー広告は、WordPressでブログを書いてお金を稼ぐのに最適な方法です。しかし、訪問者にとっては無視しやすいものでもあります。
人々はコンテンツを読むためにサイトを訪れ、サイドバーやページの他のエリアにはあまり注意を払わないかもしれません。投稿コンテンツは訪問者から最も注目されるので、広告を表示するには最適な場所です。
ということで、WordPressで投稿コンテンツ内に広告を挿入する3つの異なる方法を見てみましょう。
特定のメソッドに直接ジャンプしたい場合は、以下のリンクをご利用ください。
方法1.WPCodeを使用して投稿コンテンツ内に広告を挿入する(自動的)
投稿コンテンツ内に広告を挿入する一つの方法は、無料のWPCodeプラグインを使用することです。
WPCodeは、市場で最高のWordPressコードスニペットプラグインの一つであり、それはトップWordPress広告管理プラグインの一つとして兼ね備えています。WordPressサイトのページ、投稿日、ウィジェットエリアに広告を掲載するためのコードを簡単に挿入することができます。
Google Adsenseのような第三者の広告ネットワークからの広告を表示することができます。または、WordPressブログで広告を販売するのに最適な、独自のホスティング広告を表示することもできます。
開始するには、WPCodeプラグインをインストールし、有効化する必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。
有効化したら、WordPressダッシュボードからCode Snippets ” + Add Snippetにアクセスしてください。
ビルトインのコード・スニペット・ライブラリが表示されます。Add Your Custom Code (New Snippet)’オプションにマウスオーバーし、’Use snippet’ボタンをクリックして選択します。
次に、「Add title for snippet(スニペットにタイトルを追加)」と表示されたフィールドに入力して、広告にタイトルを付ける必要がある。
サイト訪問者はタイトルを見ることはありません。
次に、サイトに掲載する広告の種類を選ぶ必要がある。
一つのオプションは、Google Adsenseのようなプログラムが提供する広告コードを使用することです。
Google Adsense コードを使用している場合は、最もパフォーマンスの高い Google AdSense バナーのサイズとフォーマットに関するガイドを参照してください。また、WordPress で AdSense 収益を最適化する方法についての完全ガイドもあります。
もう一つのオプションは、自分で広告を作成することです。その場合、WordPressサイトに広告を表示するためにお金を払ってくれる人からコードをもらうか、自分でコードを書く必要がある。
広告コードを入手したら、コピー&ペーストして「コードプレビュー」ボックスに貼り付けるだけです。
また、画面右側のドロップダウンリストから「コードタイプ」として「HTMLスニペット」を選択する必要があります。
その後、「インサーター」セクションまでスクロールダウンする。選択できる主な挿入オプションは2つあります:
- オートインサート– 広告コードは自動的に挿入され、選択した場所にサイト上で実行されます。
- ショートコード– 広告コードは自動的に挿入されません。WordPressブログの任意の場所に手動で挿入できるショートコードを取得します。
自動インサーター」をクリックすると、投稿コンテンツ内に広告が自動的に表示されます。
次に、「Location」フィールドの隣にあるドロップダウンリストから「Insert After Paragraph」オプションを選択する。
また、「挿入番号」を変更して、スニペットを挿入する段落数を選択することもできます。例えば、最初の段落ではなく、2つ目の段落の後に広告を表示したい場合は、1を2に置き換えるだけです。
段落の後に広告を挿入するだけでなく、コンテンツの前後、投稿と投稿の間、抜粋の前後などの場所(WordPressの機能)に自動的に広告を表示することもできます。
WPCodeのプレミアムバージョンでは、商品の前後、お買い物カゴ、購入手続きページなど、WooCommerceの場所に広告を挿入することもできます。
WPCode Proはまた、開始日と終了日を指定して広告コードスニペットをスケジュールすることができます。これは、ホリデーシーズンやその他の期間限定プロモーションのために広告を表示するのに最適です。
次に、「端末タイプ」セクションまでスクロールダウンします。ここでは、あらゆる端末タイプ、デスクトップのみ、モバイルのみに広告を表示するかを選択できます。
広告コードスニペットが自動インサートされるページや投稿を制限したい場合は、「スマート条件ロジック」セクションまでスクロールダウンしてください。
例えば、特定のページタイプやページURLで広告を非表示にすることができます。
広告を整理するために、「基本情報」セクションに行くことができます。このエリアでは、タグやメモを追加して、広告コードスニペットの用途を覚えておくことができます。
また、同じ場所に複数のスニペットを表示したい場合に、スニペットの実行順序をコントロールできる「Priority」フィールドもある。
すべてのスニペットの優先順位は初期設定として10に設定されているので、広告コードを他のものより早く表示させたい場合は、優先順位を低い数字に設定すればよい。
広告の作成が終わったら、いよいよ公開しましょう。
そのためには、ページのトップに戻り、スイッチを「非活性化」から「有効化」に切り替えます。そして、『スニペットを保存』ボタンをクリックする。
これで完了です!このチュートリアルで紹介した「自動挿入」方法を選択した場合、広告が投稿コンテンツ内に自動的に挿入されます。
代わりに「ショートコード」挿入方法を選択した場合は、新規ページや投稿を作成するか、編集用のページを開きます。そして、投稿コンテンツ内でこの広告を表示したい場所を正確に探し、新しいショートコードブロックを作成するだけです。
ショートコードをこのブロックに貼り付けてください。ステップバイステップの手順については、WordPressでショートコードを追加する方法についての初心者向けガイドをご覧ください。
これで、ページや投稿にアクセスして、投稿コンテンツ内の広告を見ることができる。
注:Google Adsense広告を表示している場合、ページに広告が表示されるまで20~30分かかることがあります。
方法2.アドインサーターを使って投稿コンテンツ内に広告を挿入する。
もう一つのオプションは、Ad Inserterを使うことだ。これはWordPressの広告管理プラグインで、ホスティング広告とネットワークからプロバイダーされた広告の両方を表示することができる。
サイトネットワークを使用している場合は、Ad Inserterプラグインに広告コードを貼り付けるだけです。しかし、ホスティングサービスを利用している場合は、自分でコードを書くか、サイトに広告を表示するためにお金を払っている人からコードをもらう必要があります。
最初に行う必要があるのは、無料のアドインサータープラグインをインストールして有効化することです。WordPressプラグインのインストール方法については、こちらをご覧ください。
有効化した後、設定 ” 広告インサーター.
この画面では、タブごとに異なる広告が表示されます。これにより、複数の異なる広告を作成し、サイトに掲載することができます。
投稿コンテンツ内に表示したい広告のコードをタイプインするか、コピー&ペーストします。
次のステップは、この広告をサイトに掲載することです。一つのオプションは、ショートコードを使用して、任意のページ、投稿、またはウィジェット対応エリアに広告を追加することです。
もう一つのオプションは、アド・インサーターに、すべての投稿にこの広告を、3段落目以降など特定の場所に自動的に追加するように指示することです。
この広告を自動的に投稿に追加したい場合は、まず「投稿」ボックスにチェックを入れてください。
次に、「挿入」ドロップダウンを開き、インサーターがこの広告を表示する場所を選択する必要があります。
投稿コンテンツ内に広告を挿入したいので、通常、Before Paragraph(段落の前)、After Paragraph(段落の後)、Before Image(画像の前)、After Image(画像の後)のいずれかを選択します。
リストからオプションを選択した後、この広告を表示する場所を正確に入力します。
例えば、2つ目の段落の後に広告を表示したい場合は、次の画像にあるように「2」と入力します。
設定が完了したら、「設定を保存」ボタンをクリックしてください。
アドインサーターがこの広告を投稿コンテンツ内の選択した場所に挿入します。
先に述べたように、ショートコードを使ってサイトにこの広告を掲載することもできる。
これを行うには、’Manual’ボタンをクリックし、’Shortcode’ボックスをチェックする。
設定が完了したら、インサーターショートコードをコピーしてください。その後、「設定を保存」ボタンをクリックして広告を保存することができます。
これで、このショートコードを任意のページ、投稿日、またはウィジェット対応エリアに設置する準備ができました。ステップバイステップの手順については、WordPressでショートコードを追加する方法のガイドを参照してください。
方法3.コードを使って投稿コンテンツ内に広告を挿入する(上級者向け)
WordPressの投稿コンテンツにコードを使用して広告を追加することもできます。
サイトのコードを編集することになるので、これは最も初心者に優しい方法ではない。しかし、特別な広告管理プラグインをインストールすることなく広告を作成することができます。
コードを使用して広告を挿入するには、テーマのfunctions.phpファイルを開くか、サイト固有のプラグインを作成するか、コードスニペットプラグインを使用します。
テーマのfunctions.phpファイルを編集することなく、簡単にWordPressにスニペットを貼り付けることができるので、ここでも無料のWPCodeプラグインの使用をお勧めします。
さらに、WPCodeは、一般的なコードエラーを防止し、サイトの破壊からあなたを保護するためのスマートコードスニペット検証を付属しています。
そして次のコードを追加する:
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
このコードは、投稿コンテンツの2段落目以降に広告を表示します。
他の場所に広告を表示したい場合は、次のセクションを変更するだけです:
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 3, $content );
}
上のサンプルコードでは、3段落目の後に広告を挿入しているのがわかります。
それができたら、広告コードをタイプインするかペーストしてください。これは、あなたの広告がどのように見え、どのように動作するかをコントロールするコードです。
このコードを$ad_codeの値として追加します。$ad_codeには「Add code goes here」と書かれています。
$ad_code = '<div>Ads code goes here</div>';
これが完了したら、広告を公開する準備が整いました。これを行うには、サイト固有のプラグインを有効化するか、サイトのfunctions.phpファイルに行った変更を保存するか、WPCodeでコードスニペットを有効化して保存します。
あなたのサイトにアクセスすると、広告が表示されます。
この投稿が、WordPressで投稿コンテンツ内に広告を挿入する方法を学ぶのにお役に立てば幸いです。また、ブログのトラフィックを増やす方法や、WordPressのツールやSEOプラグインのリストもご覧ください。
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.
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!
Manuel Graf says
Thanks, it works for me, but I need a feature, can the magic only happen if there are more than 1500 characters in the Post? How can I do this?
Thanks a lot!
WPBeginner Support says
We do not have a recommended method for only activating if there are a certain number of characters but if we do we will look to update the article
管理者
Erika says
Will the Ad Sanity allow to place external ads on an individual post only not on the entire site?
WPBeginner Support says
If you wanted to you can only display ads on one post!
管理者
George says
Is there any way how to insert different ads after multiple paragraphs? For example AdSense ad no 1 after 2nd paragraph, AdSense no 2 ad after 6th paragraph?
WPBeginner Support says
If your ad network has different ads to display then method 2 should allow for what you are wanting currently
管理者
Gideon says
will this code replace the entire existing code of the theme child theme?
The <?php start is returning error
WPBeginner Support says
It will not replace your entire theme’s design. For pasting the code in your functions.php you could try removing that opening PHP tag to see if that helps solve the issue.
管理者
Marvin says
How can i make it to repeat (For Example. to repeat after paragraph 2.)?
WPBeginner Support says
We do not have a recommended method for having the code repeat at the moment.
管理者
Deepak says
Fascinating. It is so easy. Thank you guys.
WPBeginner Support says
You’re welcome
管理者
Mi Monir says
This is working. Thanks a lot.
WPBeginner Support says
You’re welcome
管理者
Oniel says
Can you please update the plugin to be able to add the code “before” the first paragraph instead of “after” the first paragraph?
WPBeginner Support says
The plugin does have the option to insert before your post content instead of after a paragraph number
管理者
Shannon says
This is amazing, thank you so much for this. Do you know why my ad goes to the left of the post and not in the middle?
WPBeginner Support says
The most common reason would be if your theme has styling that is forcing that alignment.
管理者
Karthik says
how much time take for adsense ad display on our site once we get approved after the code placed.
WPBeginner Support says
That would be a question for Adsense as they may not have ads they would currently offer for your site.
管理者
Sangam says
Thank you, with the help of this snippet I managed to show ad after each 300 words. I just added word count instead of paragraph.
WPBeginner Support says
Glad we could give you a starting point
管理者
Chango says
Hello is that Insert posts ads plug in good? or does it crash the site or slow it down? I am nervous because 7 years ago I used a adsense plug in and the maker of the plug in was adding their ads on my website.
Is this insert ads plug in good that you recommend. Does it mess with coding or slow down the site?
thanks.
WPBeginner Support says
Unless you manually add problematic code, the insert post ads plugin shouldn’t harm your site’s speed.
管理者
Arkantos says
Works great! But I noticed it also counts the paragraphs from an embedded tweet. Is it a way to avoid that and count only text paragraphs from the content/article?
WPBeginner Support says
Sadly, not at the moment.
管理者
Von says
Is there a wordpress tag to have a ‘content/block’ automatically injected into the middle of my blogs.
I’m looking for documentation (I’d seen it before). I would appreciate any help here.
Thank you.
WPBeginner Support says
For what it sounds like you’re wanting, you would want to use the recommendation from the article
管理者
Willys says
Hi, I have just Signed up to Google adsense but I am having a problem of how to copy my Google adsense code to my website (WordPress). How can i do it?
WPBeginner Support says
For adding Adsense you would want to take a look at our article here: https://www.wpbeginner.com/beginners-guide/how-to-add-google-adsense-to-your-wordpress-site/
管理者
Sangam says
I am using this code snippet in my child theme function.php to make ads appear in 3rd paragraph. But I want it to repeat after each 3 or 4 paragraph. How it can be done?
WPBeginner Support says
As we state in the article, you would change the number in the code snippet to change where the ad appears.
管理者
Sangam says
How to repeat? After 3rd/6th/9th/12nd etc?
WPBeginner Support says
If you wanted the ad to display multiple times then inside the if statement where it has: return prefix_insert_after_paragraph
You would want a new line where you add a copy of the prefix_insert_after_paragraph with a different paragraph number
Ahsan Ali says
How to insert ad after each & every 3rd paragraph in a single post?
Kindly guide!
WPBeginner Support says
You would need to use the site-specific plugin method and change the 2 to a 3 on line 12
管理者
Peter says
Hi
Only the code is showing on my post no images. The code is just lying there in the post, you won’t even notice it’s there
What can I do to change this
I need the banners showing in the post
WPBeginner Support says
You may want to ensure you added the code with all of it’s tags correctly for your ads and didn’t skip any tags.
管理者
Ngo says
Hello,
Thank you for the manual code. It seems to be working for everyone but I just can’t get it to work on my site. My site is made up of only Custom Post Types. Is this why it is not working? Please what do I need to change in the code to make them display on my custom post types?
Even ordinary widgets do not display on my site. Is there anything I can include in my functions.php to make changes display on my custom pages?
Please help, I have tried every code on the internet and none of them works.
WPBeginner Support says
If you want to use the custom code on a custom post type and the method we have is not working, you could try changing is_single() to: is_singular( $post_types = ” )
and place the name of your custom post type in the single quotes
管理者
Swagatam Innovations says
It worked like charm, thanks very much for this code!
WPBeginner Support says
Glad our article could help
管理者
Minh Hieu says
Can you help me?
How to insert ads before an ID element?
Ex: i want to insert ads before
Thanks you very much!
JOHN HOBBS says
Thanks for everything you provide. It’s truly appreciated. But question. This article reads
Last updated on May 2nd, 2014
at the top. Is the information presented still valid and up-to-date?
You guys have been a tremendous help to me so I feel bad asking but I need to know.
Again, thanks for the great resources you provide!
John
WPBeginner Support says
Hi John,
The plugin mentioned in the article is still available and works great.
管理者
Tell.com.ng says
Please I need a clearly defined way to add more than one ad after different paragraphs.
Reply needed urgently please.
Thanks!
Atul says
i am using infinite scroll featured theme and post advert plugin for ads but ads show only on first post, not next post. how can i solve it. this plugin does not repeat ads between post.
Melissa says
This is by far the easiest plugin I’ve ever used! It works perfectly and looks great. Thank you so much! WPBeginner has been a huge help to me in starting my new blog!
rishabh shrivastava says
It was helpful! Thanks
sandeep says
thank you so much its working dude
Hoàng Trí says
So I Want to insert ads by tag (Like Gym, Fitness) only two tag will show ads, and other will not. How i can do it ?
Bob says
How can I place an ad before the first paragraph..underneath the single post image? When i set the number to”0″…nothing happens?
Qasim Iqbal says
Hey, If I added the code using Post advert, would it be on every post? right?
If not then tell me, I want that
Bright Joe says
Thank you so much for the code for the functions.php. This just lets me avoid so many unnecessary plugins.
Thanks again!
Martijn says
Hi,
Thanks for the code!
How can I exclude certain posts?
Greetings,
Martijn
David says
Was sincerely helpful to start off for my presentation at Kampala WordCamp 2017.
Really incredible help from you.
Segun says
how can i insert HTML code in my ad space?
Adem says
for more as one ad, change to this:
$ad_code = ‘Ads code goes here’;
$ad_code2 = $ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $ad_code, 2, $content );
$content = prefix_insert_after_paragraph( $ad_code2, 8, $content );
return $content;
}
return $content;
}
Thanks!
Bright Joe says
Thank you so much!
Akios says
Don’t know how to insert it
Sunil Kumar says
Thanks! It worked.
Shehab says
I want to show two ads on my post. How can I do it?
That process you showed, here I can see only one ad on every post. but I want to show two ads in every post
shuseel says
I want to implement ads code other than Adsence code using JavaScript code.
The above code does not work when I have replaced JavaScript code on Ads code goes here.
Please suggest me how to implement
on the place of text.
phil says
Hi,
Congratulation for this function.php code ! Works well
I ve a question :
How can i do for do not display adds on specific posts or pages ?
Many thx
Regards
Phil
Andrea says
Nice code! How insert only for a certain author post?
Aditya says
I am using Admiral theme and I want to display ads above the thumbnail of every post. What can I do ?
Shubhamk says
I installed the plugin, I created my ads in the post but it appears at the bottom, not at the start.
Sally says
Hi, I installed the plugin on my wp site and followed the instructions – post ad and edit settings to show ads on posts. But the ads don’t show on my posts. Do you know why? thanks in advance!
Gail says
Have just started using the plug-in and it works great!! A question: I now want to insert a new ad. Do I have to trash the old one and then create a new one? Or can I reserve the old one for a future use? Seems like I do have to trash it to make room for the new one. Thanks.
Percival says
I am wondering if it’s possible to insert a widget, rather than $ad_code = ‘Ads code goes here’;
Is this possible? I can’t seem to implement the syntax correctly, as inserting a widget seems not to respect the paragraph numbering, and randomly inserts a “1” into the text:
$ad_code .= dynamic_sidebar(‘test’);
John says
Percival, did you figure out a solution? Trying to do something similar.
Morgan says
Where do I get a code for an ad .?
hassan says
did you mean code for ads?………
you have to visit your ads network be it google adsense or for your ads code then insert it between insert here……….
Stephen says
You have to make a Google Adsense account and make an ad within that. Then, you take the code they give you for the newly created ad and insert it there.
Jagadish Prasad B says
I want to put my ads in the middle of the content by using an editor. Pleas e suggest me how to add the code in the editor. Thanks for the support.
Ryan S says
Creating shortcode is the best fit on your end, so first, you need to create Shortcode ads and then use that shortcode in your article/post.
Ryan S
hsabarp says
thanks
this is the what i was searching for…….
Vỹ Spirit says
Hello, After I add the above code to the function.php file, my article does not display ads, instead, and it’s adsbygoogle text …. How do I fix it?
hassan says
i did mine some minutes ago and it work well try to remove the <?php if your functon.php already have it…….
Enrique Grobbelaar says
This app seems to work for everyone, except me.
I have followed all the steps, said it should display after paragraph 3. Now there is a large white block on all posts below paragraph 3, but no ad.
Does somebody perhaps know what I am doing wrong?
(Using Google Adsense)
Enrique Grobbelaar says
Never mind. It is working now.
Great says
Thanks you for the tutorials, I used the post ad on my site is working perfectly well but can not open in a new window. Pls how do I set the post ad banners to open in a new window?