WordPress.org

Ready to get started?Download WordPress

Forums

Insert google ad inbetween first and second post (20 posts)

  1. charles
    Member
    Posted 9 years ago #

    On some blogs, I've seen folks insert their google ad inbetween the first and second post on the main page. How would I go about doing this? Right now, I ususally put my Google code in the sidebar, but I'd like to insert in between the first and second post instead.
    I imagine I would have to modify part of the Loop in my index.php, but I don't know where to start. I'm using the cool Kubrick template which has the following in the index.php:
    [code]
    <?php if ($posts) { foreach ($posts as $post) { start_wp(); ?>
    <h2 id="post-<?php the_ID(); ?>">" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></h2>
    <small>/<?php the_time('m') ?>/<?php the_time('j') ?>/"><?php the_time('l') ?>, /<?php the_time('m') ?>/"><?php the_time('F') ?> /<?php the_time('m') ?>/<?php the_time('j') ?>/"><?php the_time('jS') ?>, /"><?php the_time('Y') ?> by <?php the_author() ?> <?php edit_post_link('Edit','(',')'); ?></small>
    <div class="storycontent">
    <?php the_content('Read the rest of this entry »'); ?>
    </div>
    <p class="postmetadata">Posted in <?php the_category(',') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »','permanent.php'); ?>
    <!--
    <?php trackback_rdf(); ?>
    -->
    <?php } // closes printing entries with excluded cats ?>
    <?php } else { // end foreach, end if any posts ?>
    Sorry, no posts matched your criteria.
    <?php } ?>
    [/code]
    Any help would be appreciated. Thanks!

  2. davidchait
    Member
    Posted 9 years ago #

    BEFORE the if ($posts) line, add:
    <?php $postCounter = 0; ?>
    AFTER the if ($posts) line add:
    <?php
    $postCounter++;
    if ($postCount==2) { /* do ad here */ }
    ?>

    -d

  3. charles
    Member
    Posted 9 years ago #

    David, thanks for your help. It is complaining about an "unexpected '<' in the line where the google script starts. Here is my modified code:
    <?php $postCounter = 0; ?>
    <?php if ($posts) { foreach ($posts as $post) { start_wp(); ?>
    <?php $postCounter++; if ($postCount==2) (<script type="text/javascript"><!--
    google_ad_client = "pub-6133397951787340";
    google_ad_width = 468;
    google_ad_height = 60;
    google_ad_format = "468x60_as";
    google_ad_channel ="";
    google_color_border = "999999";
    google_color_bg = "FFFFFF";
    google_color_link = "0000FF";
    google_color_url = "008000";
    google_color_text = "000000";
    //--></script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>) ?>

    Again, thanks for your help.

  4. charles
    Member
    Posted 9 years ago #

    hmm... it's not letting me post my google code in the forum...

  5. Anonymous
    Unregistered
    Posted 9 years ago #

    i am getting a parse error saying tat it requires '<' in line 54.
    -abhi.

  6. Anonymous
    Unregistered
    Posted 9 years ago #

    this script doesn't seem to work at all... can anybody provide the correct solution.
    thank you.
    -abhi.

  7. NuclearMoose
    Member
    Posted 9 years ago #

    There is a plugin called "Moose Candy" that will allow you to place virtually any content in between posts. You can decide what and where to place this stuff.
    Currently I use it on my own site, at http://blog.nuclearmoose.ca but I'm using it only for text-based quotes. No reason why you couldn't edit the file and have whatever you like be inserted into the DIV.
    Moose Candy.

  8. Anonymous
    Unregistered
    Posted 9 years ago #

    moose candy rocks... i just installed it on my site n it works like a breeze... :)
    -abhi.

  9. davidchait
    Member
    Posted 9 years ago #

    Sorry, I assume people have some idea of PHP when they ask for these things! ;)
    BEFORE the if ($posts) line, add:

    <?php $postCounter = 0; ?>

    AFTER the if ($posts) line add:

    <?php $postCounter++; if ($postCount==2) { ?>
    .. do HTML/script here ..
    <?php } ?>

    -d

  10. unteins
    Member
    Posted 9 years ago #

    MooseCandy is pretty neat......yeah baby ;)

  11. Anonymous
    Unregistered
    Posted 9 years ago #

    I couldn't get moosecandy to work with code inserted, it was fine with just text though - what should I be doing?

  12. ErikThulin
    Member
    Posted 9 years ago #

    I tried David's code:
    <?php $postCounter = 0; ?>

    AFTER the if ($posts) line add:

    <?php $postCounter++; if ($postCount==2) { ?>
    .. do HTML/script here ..
    <?php } ?>

    And it is not giving me errors, but nothing is showing up.
    My site is: http://blog.freedomdown.net/ . Thanks for your help in advance ;).

    - Erik

  13. ColourDreamer
    Member
    Posted 9 years ago #

    Same as Eric. And Moose Candy can't be found.

  14. davidchait
    Member
    Posted 9 years ago #

    You don't need to hand-enter code any more. I released my CG-Inbetween plugin to enable easily doing these kinds of things.

    I've been busy, and can't recall if I updated the site to have the latest and greatest code -- if you find anything not working, ping me at cgcode at chait dot net, and I'll get right on it... (my paying work comes first! ;) ).

    -d

  15. Paola
    Member
    Posted 9 years ago #

    The adsense wordpress plugin allows you to add google ads to whatever posts you want (up to the limit of 3 per page imposed by google).

    Alternatively, the adrotator wordpress plugin allows you to rotate google adsense and other ads within the same space.

    I've used both. The adsense adds a quicktag to the post write area, just click to add the code at the end of your posts.

    I use the adrotator on one site for non google ads (though it can be used for google also). For that I added the php ad call function (specified in the plugin instructions) into the post.php file in the theme I'm using. It shows the ads after every post. If you have a front page with 15 posts, only 3 will have google or can have google. This plugin allows you to have 12 additional ads on the page.

    I love them both, and I'm a total non techie -- just followed the instructions and used some common sense and trial and error. Both were figured out quickly. No need to mess with code -- good grief that would fry me.

  16. davidchait
    Member
    Posted 9 years ago #

    CG-Inbetween was built to insert text/html (i.e., javascript ad code), included file (i.e., htm/php file), or simple php function call output, after specified posts on a page. All you do is add one line of code to your template, the rest is done through an admin interface.

    It's great for inserting things after say the 3rd, 6th, and 9th posts -- or whatever post ID's you want. Also, the include functionality makes it easy to keep your adsense in an HTML file, in case you want to tweak it, but then 'attach' it off of a few post indices.

    It also has the concept of 'last post' on the page, good for having a particular ad/file inserted at the end of each page's worth of posts. It can also go inbetween all posts, good for playing with dividers without constantly touching code.

    Anyway, if people have questions, feel free to ask! ;)

    -d

  17. ColourDreamer
    Member
    Posted 9 years ago #

    Can I use this to show a different number of posts in my archive pages than on the index? or vice versa.

  18. Beel
    Member
    Posted 9 years ago #

    There is a plugin specifically for that, I believe, and written by one of the devs, as I recall.

    Edit: Yep, by Ryan no less... "Custom Posts Per Page"

  19. Kafkaesqui

    Posted 9 years ago #

  20. Beel
    Member
    Posted 9 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.