Support » Fixing WordPress » How to put adsense in the “loop” in the index.php file..

  • Hello,

    I am trying to put adsense between individual posts on the main page and would also like them to appear in categories and single permalink pages.

    My “loop” looks like this…

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php require(‘post.php’); ?>
    <?php endwhile; ?>

    Could someone provide me with the exact code I need to use to for example put adsense after the second post and after the third post as an example?

    My wordpress blog is at http://www.technobunga.com and I really want to adjust my ads.

    Additionally, for some reason I cannot eliminate the extra space between my header image and the very top of the page. I want to move everything up so people can see more content. Does anyone know how I can do that?

    Thank you very much for any help you can provide.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Can anyone help me with this?

    not exactly sure about the first problem, my guess is that you would make a function to write your adsense ad in every say… 4 posts (based on an item or loop count) and place a call to that function in The Loop.

    Second problem, in your css, you have

    #page {
    background-color: white;
    margin: 5px auto;
    padding: 0;
    width: 900px;
    border: 2px solid #306EFF;
    }
    change
    margin: 5px auto;
    to
    margin: 0px auto;

    hope that helps

    oh one more thing, all the ads on your page make your page lag like hell when scrolling and searching and doing a live css edit, just FYI.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Quick and easy way to do it:

    <?php $post_counter=0; ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php require('post.php'); ?>
    <?php
    $post_counter++;
    if ($post_counter == 2 || $post_counter == 3) { ?>
    INSERT ADSENSE CODE HERE
    <?php } ?>
    <?php endwhile; ?>

    Or, use my CG-Inbetween plugin:
    – upload and activate plugin
    – add the inbetweenPosts() call to your index.php inside the end of the post-content block (whether it’s before or after any post-meta info is up to you).
    – create an html file with your adsense code, assuming you just want one format (create two files if two formats…)
    – create an Inbetween after 2, and another Inbetween after 3, using the File Include option to include the html file from previous step. If the same html file, you can actually use the shorthand and have the inbetween at position “2,3” (minus the quotes) to have one inbetween at two locations.

    View site and enjoy. 😉

    CG-Inbetween can also be set to display after ‘last’, to always have an ad at the end of the posts (including single posts), or specific ads for after ‘single’ posts.

    -d

    Hi;
    sorry to bring this up again,
    I like to try this script,
    I don’t know much about php, but here is question I have,
    I want to have it this way:
    add ad sense after certain amount of word insead of post,

    the ad should be inserted within content, say after 200 word.
    of course I don’t mean ad senes just any ad, and it will be on every post, as long as there is post it will be a insertion, even two or three ads if the content is long enough.

    anyone willing to help me with code change please?

    I have pasted the above code into the index page of theme( default), to just try this out, but becasue the page has already a condion expression “if” so the “endwhile” function will cause the problem, becasue there is one already there, even I took off the endwhile from the above code, but there is an error of that it is :unexpected $end in line 38, which is at the end of the page.
    here is the whole code from index file of theme:

    <?php get_header(); ?>
    <div id=”content” class=”narrowcolumn”>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h2>
    <small><?php the_time(‘F jS, Y’) ?> <!– by <?php the_author() ?> –></small>
    <div class=”entry”><?php the_content(‘Read the rest of this entry »’); ?>

    <?php $post_counter=0; ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php require(‘post.php’); ?>
    <?php
    $post_counter++;
    if ($post_counter == 2 || $post_counter == 3) { ?>
    INSERT ADSENSE CODE HERE
    <?php } ?>
    <?php endwhile; ?>

    </div>
    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?>
    </div>
    <?php endwhile; ?>
    <div class=”navigation”>
    <div class=”alignleft”><?php next_posts_link(‘« Previous Entries’) ?></div>
    <div class=”alignright”><?php previous_posts_link(‘Next Entries »’) ?></div>
    </div>
    <?php else : ?>
    <h2 class=”center”>Not Found</h2>
    <p class=”center”>Sorry, but you are looking for something that isn’t here.
    <?php include (TEMPLATEPATH . “/searchform.php”); ?>
    <?php endif; ?>
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    thanks for any help.

    I have custom code that does checks for linkbreaks in the post content and can insert an ad during the content generation pass — but I never tried to extract it into a plugin for general use (I get few enough donations for my work as it is! ;)).

    I think Adsense-Deluxe is suited towards in-post ad insertions, if you want adsense stuff. You might look at that.

    -d

    thanks davidchait for the reply,

    yes I have checked that out, but that is manual,
    it would be nice to have one as loop, you could have full rss article and this way you don’t have to come back and insert ad.
    can someone help with even a word count, istead of echo the number of words in the end of every post, put a if condition that when words are => than a number then echo a div code or even call a text file to echo.
    I know this is way to easy for a good php coder but I have tried to work around a wordcount that already available in wp but as my php is not very good, I could not make it work.

    thanks

    Hi,
    Joe L.Golson here

    I need help with the adsense plug-in.Everything is fine until I try to configure the optins for the adsense blocks.
    When I try to save the optins,that’s when get this message
    in the blue shaded area :

    Missing Keyword or Code value; Nothing added.

    Can you help me to figure this out,

    Joe L

    Missing Keyword or Code value; Nothing added.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to put adsense in the “loop” in the index.php file..’ is closed to new replies.