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

Så här infogar du annonser i ditt posts content i WordPress

Inga annonser spelar en stor roll när det gäller att generera intäkter, men de kan också skada användarnas engagement om de inte används korrekt.

Alltför påträngande eller dåligt placerade annonser kan distrahera eller irritera användare. Denna distraktion kan potentiellt minska övergripande metrics för engagement, såsom tid på site eller avvisningsfrekvens.

I den här guiden kommer vi att gå igenom stegen för att strategiskt infoga annonser i ditt post content i WordPress.

Om du är new när det gäller att tjäna pengar på din site eller vill optimera annonsplaceringen, kommer vi att ge experttips och Step-by-Step-instruktioner som hjälper dig att visa annonser utan att skada användarupplevelsen.

How to insert ads within your post content in WordPress

Varför infoga annonser i ditt post content i WordPress?

Banner- och sidebar-annonser är ett bra sätt att tjäna pengar på att blogga online med WordPress. De kan dock lätt förbises.

Människor besöker din site för att läsa ditt content och kanske inte ägnar lika mycket uppmärksamhet åt din sidebar eller andra areas på sidan. Eftersom innehållet i ditt post får mest uppmärksamhet från besökare är det den perfekta platsen att visa annonser.

Med detta i åtanke kommer vi att visa dig 3 olika sätt att infoga annonser i ditt post content i WordPress. Om du föredrar att hoppa direkt till en viss metod kan du använda länkarna under:

Är du redo? Låt oss komma igång.

Metod 1. Infoga annonser i inläggets innehåll med WPCode (automatiskt)

Ett sätt att infoga annonser i ditt post content är genom att använda det gratis tillägget WPCode. Det är ett av de bästa WordPress code snippets-pluginsen på marknaden och fungerar även som ett av de högst upp pluginsen för hantering av WordPress-annonser.

WPCode tillåter dig att enkelt infoga kod för att placera annonser på någon av dina WordPress webbplats sidor, posts, eller widgetar områden.

Du kan visa annonser från tredjepartsannonsnätverk som Google Adsense. Alternativt kan du visa dina egna hosted annonser, vilket är perfekt för att sälja annonser på din WordPress blogg.

För att komma igång måste du installera och aktivera plugin-programmet WPCode. Om du behöver hjälp, vänligen se vår guide om hur du installerar ett plugin för WordPress.

Efter aktivering, låt oss gå till Code Snippets ” + Add Snippet från din WordPress dashboard.

Detta tar dig till det built-in biblioteket med code snippets. Håll bara hovern över alternativet ”Add Your Custom Code (New Snippet)” och klicka på knappen ”Use snippet” för att välja det.

Choose the Custom Code option in the WPCode library to insert an ad

Därefter måste du ge din annons en rubrik genom att skriva in i fältet som visar ”Add title for snippet”.

Obs/observera att dina besökare inte kommer att se titeln eftersom den bara är till för din referens.

Add title for your ad code snippet in WPCode

Efter det kan du välja vilken typ av annons du vill placera på din site.

Ett alternativ är att använda annonskoden som tillhandahålls av ett program som Google AdSense.

Om du använder Google Adsense-kod kanske du vill se vår guide om de mest utförda Google Adsense-bannerstorlekarna och -formaten.

Vi har också en komplett guide till hur du optimerar dina AdSense-intäkter i WordPress.

Ett annat alternativ är att skapa din egen annons. Om du väljer den här metoden måste du få koden från den person som betalar dig för att visa annonsen på din WordPress site eller skriva koden själv.

När du har annonskoden kopierar du bara och klistrar in den i boxen ”Code Preview”. Då måste du också välja ’HTML Snippet’ som ’Code Type’ från dropdown-listan till höger på vyn.

Copy and paste ad code into Code Preview box

I nästa steg vill du rulla ner till sektionen ”Insertion”. Det finns två huvudsakliga alternativ för infogning som du kan välja mellan:

  • Auto-Insert – Annonskoden infogas automatiskt och körs på din website på den valda locationen.
  • Shortcode – Annonskoden kommer inte att infogas automatiskt. Du kommer att få en shortcode som du manuellt kan insert var som helst på din WordPress blogg.

Du kan klicka på metoden ”Auto-Insert” för att automatiskt visa annonser i innehållet i posten.

Gå sedan vidare och välj alternativet ”Infoga efter stycke” från dropdown-listan bredvid fältet ”Location”.

Select Auto-Insert Insert After Paragraph to insert ad within post content

Du kan också ändra ”Insert Number” för att välja antalet stycken efter vilka snippet ska infogas.

Om du till exempel vill visa annonsen efter det andra stycket i stället för det första ersätter du bara 1 med 2.

Notis: Förutom att infoga annonser efter ett stycke kan du också automatiskt visa annonser på locations (WordPress-funktioner), t.ex. före eller efter content, mellan posts, före eller efter excerpt m.m.

Additional auto-insert locations in WPCode

Med premiumversionen av WPCode kan du infoga annonser på WooCommerce-platser som före eller efter produkter, cart och checkout page, bara för att nämna några.

WPCode Pro låter dig också schemalägga dina annons code snippets med ett start- och slutdatum. Detta är perfekt för att visa annonser runt semesterperioden eller för andra kampanjer med begränsad tid.

Därefter kan du rulla ner till ”Device Type” -sektionen. Här är du välkommen att välja om du vill visa din annons på alla typer av enheter, endast skrivbord eller endast mobil.

Choose which device types you want to display the ad on using WPCode

Om du vill limitera de pages eller posts där ad code snippet infogas automatiskt kan du rulla ner till ”Smart Conditional Logic” section.

Du kan till exempel dölja dina annonser på specifika sidtyper eller URL:er till sidor.

Use WPCode Conditional to limit pages where the ad appears Logic to

För att hjälpa dig att organisera dina annonser kan du gå till ”Basic info” section.

I detta area kan du add to taggar och obs/observera för att hjälpa dig att komma ihåg vad ditt code snippet är till för.

Add tags to organize your ad code

There’s also a ”Priority” field that allow you to control the order in which the snippets are executed when you want to display multiple snippets in the same location.

All snippets får en prioritet på 10 som standard. Men om du vill att din annonskod ska visas tidigare än andra ställer du helt enkelt in prioriteten på ett lägre tal.

När du har slutfört arbetet med din annons är det dags att publicera den.

För att göra det, låt oss gå tillbaka högst upp på sidan och toggle omkopplaren från ’Inaktiverad’ till ’Aktiv’. Klicka sedan på knappen ”Save Snippet”.

Activate and save ad code in WPCode plugin

Nu är det klart! Om du valde metoden ”Auto-Insert” som visas i den här tutorialen kommer din annons nu automatiskt att infogas i innehållet i ditt post.

Om du väljer metoden för infogning av ”Shortcode” kan du antingen skapa en ny page eller post eller öppna en för edit. Sedan hittar du helt enkelt den exakta platsen där du vill visa den här annonsen i ditt postinnehåll och skapa ett nytt shortcode-block.

Du kan sedan gå vidare och klistra in shortcoden i detta block. För steg-för-steg-instruktioner kan du läsa vår guide för nybörjare om hur du lägger till en shortcode i WordPress.

Nu, om du besöker sidan eller inlägget, ser du annonsen i ditt postinnehåll.

Note: Om du visar Google Adsense-annonser kan det ta 20-30 minuter innan annonsen visas på page.

Metod 2. Infoga annonser i postens innehåll med hjälp av Ad Inserter

Ett annat alternativ är att använda Ad Inserter. Detta är ett plugin för hantering av WordPress-annonser som kan visa både hosted-annonser och annonser som tillhandahålls av ett nätverk.

Om du använder ett nätverk, kan du helt enkelt klistra in din annonskod i Ad Inserter tillägget. Men om du skapar en hosted annons, måste du antingen skriva koden själv eller få den från den person som betalar för att visa sin annons på din website.

För att komma igång är det första du behöver göra att installera och aktivera det gratis pluginet Ad Inserter. Om du behöver hjälp, vänligen se vår guide om hur du installerar ett plugin för WordPress.

När du är aktiverad vill du gå till Settings ” Ad Inserter.

The Ad Inserter WordPress plugin

På den här vyn representerar varje tabs en annan Advertising. This allows you to create multiple ads and place them on your website.

Du kan nu antingen skriva in eller kopiera / klistra in koden för den annons som du vill visa i innehållet i ditt inlägg.

Insert ads within your post content in WordPress using Ad Inserter

Nästa steg är att placera denna annons på din website.

Ett alternativ är att add to annonsen till en page, post eller widget-ready area med hjälp av en shortcode. Ett annat alternativ är att berätta för Ad Inserter att automatiskt lägga till denna annons till varje post och på en viss location, till exempel efter vart tredje stycke.

If you want to add this advertisement to your posts automatically, then you can start by checking the ’Post’ box.

The Ad Inserter advert management plugin

Därefter måste du öppna dropdown ’Insertion’ och välja var Ad Inserter ska visa den här annonsen.

Eftersom vi vill infoga annonser i innehållet i posten, vill du vanligtvis välja antingen ’Before Paragraph’, ’After Paragraph’, ’Before Image’ eller ’After Image’.

Placing the ad within your WordPress post content

När du har valt ett alternativ från listan kan du skriva in exakt var den här annonsen ska visas.

Om du till exempel vill visa annonsen efter andra stycket skriver du in ”2”, som du kan se i följande image.

Inserting ads within your post content in WordPress

När du är nöjd med dina inställningar klickar du på knappen ”Save Settings”.

Inga annonser kommer nu att insert denna annons i ditt post innehåll på din valda location.

Saving your ad settings

Precis som vi nämnde tidigare kan du också placera den här annonsen på din webbplats med hjälp av en shortcode.

För att göra detta klickar du på knappen ”Manual” och kontrollerar sedan boxen ”Shortcode”.

Adding an ad to your post content using shortcode

När du har gjort det kopierar du bara kortkoden för Ad Inserter. Du kan sedan klicka på knappen ”Save Settings” för att spara din annons.

När du är klar är du redo att placera denna shortcode på valfri page, post eller widget-ready area. För Step-by-Step-instruktioner kan du se vår guide om hur du lägger till en shortcode i WordPress.

Metod 3. Infoga annonser i postens innehåll med hjälp av kod (avancerad)

Du kan också add en annons till ditt post content i WordPress med hjälp av kod.

Eftersom du redigerar koden på din webbplats är detta inte den mest nybörjarvänliga metoden. Det tillåter dig dock att skapa annonser utan att behöva installera ett speciellt plugin för annonshantering.

För att insert en annons med hjälp av kod, öppna antingen ditt temas functions.php-fil eller använd ett code snippets plugin.

Återigen rekommenderar vi att du använder det gratis WPCode-pluginet eftersom det gör det enkelt att klistra in snippets i WordPress utan att editera ditt temas functions.php-fil.

Dessutom kommer WPCode med smart validering av code snippet för att hjälpa dig att förhindra vanliga kodfel och förhindra att du bryter din webbplats.

Du kan sedan add to följande kod:

//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 );
}

Denna kod kommer att visa en annons efter andra stycket i ett posts content.

Om du vill visa din annons på någon annan location, behöver du bara ändra följande section:

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 3, $content );
    }

I exempelkoden ovan kan du se att vi infogar annonsen efter det tredje stycket.

När du har gjort det kan du gå vidare och antingen skriva in eller klistra in din annonskod. Det här är koden som kontrollerar hur din annons ser ut och fungerar.

Du lägger till den här koden som $ad_code-värdet, där det står: ”Add code goes here.

    $ad_code = '<div>Ads code goes here</div>';

När du har gjort det är du redo att publicera din annons.

För att göra detta aktiverar du antingen ditt site-specifika plugin, sparar de ändringar du har gjort i din sites functions.php-fil eller aktiverar och sparar ditt code snippet i WPCode.

Nu, om du besöker din website, bör du se din annons live.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du infogar annonser i ditt post content i WordPress. Du kanske också vill lära dig hur man gör en classified ads website eller kontrollera våra expertval av de bästa tip jar-tilläggen för 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.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

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.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

215 kommentarerLämna ett svar

  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. 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 :)

      Administratör

  3. 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 :)

      Administratör

  4. 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.

      Administratör

  5. 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

      Administratör

  6. 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.

      Administratör

    • WPBeginner Support says

      That would be a question for Adsense as they may not have ads they would currently offer for your site.

      Administratör

  7. 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.

  8. 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.

      Administratör

  9. 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?

  10. 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

      Administratör

  11. 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?

  12. 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.

      Administratör

        • 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

    • WPBeginner Support says

      You would need to use the site-specific plugin method and change the 2 to a 3 on line 12

      Administratör

  13. 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.

      Administratör

  14. 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

      Administratör

  15. 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!

  16. 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

  17. 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!

  18. 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.

  19. 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! :)

  20. 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 ?

  21. 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?

  22. 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

  23. 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!

  24. David says

    Was sincerely helpful to start off for my presentation at Kampala WordCamp 2017.
    Really incredible help from you.

  25. 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!

  26. 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

  27. 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.

  28. 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

  29. 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!

  30. 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.

  31. 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’);

    • 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.

  32. 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

  33. 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…….

  34. 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)

  35. 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?

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.