WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Putting an ad under just the FIRST post?

[Resolved] Putting an ad under just the FIRST post?

  • How would I go about putting an advertisement under just the FIRST post on the page?

    Also, I would make it so it only showed up when a Guest viewed the site and not when a registered user is looking at the page?

Viewing 15 replies - 1 through 15 (of 18 total)
  • Your theme has something like this in its index file:

    <?php while (have_posts()) : the_post(); ?>
      <div class="post">
        <!-- post-level stuff here -->
      </div>
    <?php endwhile; ?>

    In WordPress lingo, it’s called The Loop. What you need is to modify it along these lines:

    <?php 
    
    // This part is responsible for retrieving information
    // about the current user and can be placed anywhere
    // before The Loop:
    global $user_ID;
    get_currentuserinfo();
    
    ?>
    
    <?php while (have_posts()) : the_post(); ?>
      <div class="post">
        <!-- post-level stuff here -->
    <?php
      if ('' == $user_ID) {
        // user not logged in
        if (!is_defined('AD_SHOWN')) {
          // show ad
          define('AD_SHOWN', true);
        }
      }
    ?>
      </div>
    <?php endwhile; ?>

    How would this work though?

    Exactly as written in the code. 🙂

    First, WordPress would check if the user is logged in.

    Then, assuming the user is not logged in, every time WordPress renders a post, it would check if a constant named AD_SHOWN is defined. If it’s not (meaning, it’s the first post being rendered), WordPress would display the ad and define the constant. When posts past first are rendered, the constant will already be defined, so no additional ad displays will take place.

    But please tell me, what part of the script tells WordPress to show the ad under JUST the first post on the page? I don’t understand that part. I know the whole guest/register thing already.

    what part of the script tells WordPress to show the ad under JUST the first post on the page?

    When the first post is displayed, AD_SHOWN is not yet defined, so !is_defined('AD_SHOWN') evaluates to true and the ad can be displayed. After the ad is displayed, AD_SHOWN is defined, so !is_defined('AD_SHOWN') evaluates to false and no more ads are shown…

    And where do I stick the ad’s HTML?

    right where it currently says:

    //show ad

    Indeed. Something like this:

    <?php
      if ('' == $user_ID) {
        // user not logged in
        if (!is_defined('AD_SHOWN')) {
    ?>
    <div id="myAd">
      <!-- Ad goes here... -->
    </div>
    <?php
          define('AD_SHOWN', true);
        }
      }
    ?>

    Thanks for clarification, HandySolo!

    It give me errors when I do that, like unexpected < or T something.

    Here’s my original code:

    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    <table width="100%" border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td colspan="2"><table border="0" cellpadding="3" cellspacing="0" bgcolor="#EFFF0C">
                    <tr>
                      <td><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></strong></td>
                    </tr>
                </table></td>
              </tr>
              <tr>
                <td colspan="2"><p><?php the_content('Read the rest of this entry &raquo;'); ?></p></td>
              </tr>
              <tr>
                <td><table border="0" cellpadding="3" cellspacing="0" bgcolor="#1195FF">
                    <tr>
                      <td><a href="#"><strong><?php comments_popup_link('0 Comments', '1 Comment', '% Comments;'); ?></strong></a></td>
                    </tr>
                </table></td>
                <td><table border="0" align="right" cellpadding="3" cellspacing="0" bgcolor="#AFFF0A">
                    <tr>
                      <td><a href="#"><strong> Posted by <?php the_author_nickname(); ?> @ <?php the_time('d/m/y g:i A') ?></strong></a></td>
                    </tr>
                </table></td>
              </tr>
            </table>
    		  <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
                <tr>
                  <td height="10"></td>
                </tr>
              </table>
    
                    <?php endwhile; ?>
    
    	<?php else : ?>
    
            <?php endif; ?>
    
    <?php get_footer(); ?>

    So where do you want the ad?

    Anyway, try this, see what happens:

    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <table width="100%" border="0" cellspacing="0" cellpadding="5">
      <tr>
        <td colspan="2">
          <table border="0" cellpadding="3" cellspacing="0" bgcolor="#EFFF0C">
            <tr>
              <td><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></strong></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td colspan="2"><?php the_content('Read the rest of this entry &raquo;'); ?>
    </td>
      </tr>
      <tr>
        <td>
          <table border="0" cellpadding="3" cellspacing="0" bgcolor="#1195FF">
            <tr>
              <td><a href="#"><strong><?php comments_popup_link('0 Comments', '1 Comment', '% Comments;'); ?></strong></a></td>
            </tr>
          </table>
        </td>
        <td>
          <table border="0" align="right" cellpadding="3" cellspacing="0" bgcolor="#AFFF0A">
            <tr>
              <td><a href="#"><strong> Posted by <?php the_author_nickname(); ?> @ <?php the_time('d/m/y g:i A') ?></strong></a></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    
    <?php
      if ('' == $user_ID) {
        // user not logged in
        if (!is_defined('AD_SHOWN')) {
          echo <<<SHOWTHEAD
    
    <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
      <tr>
        <td>Ad goes here... </td>
      </tr>
    </table>
    
    SHOWTHEAD;
          define('AD_SHOWN', true);
        }
      }
    ?>
    
    <?php endwhile; ?>
    <?php else : ?>
    
    <h2>Error</h2>
    <p>The page that you wanted<br />
    Could not be found,<br />
    But countless more exist.</p>
    
    <?php endif; ?>
    <?php get_footer(); ?>

    If you get an error, post it here verbatim, including the line number at which it occurred.

    hello I m not a geek but i read lot of on php maybe you must use this

    </head>
    <body>
    <table border="0" cellpadding="0" cellspacing="0" class="d">
    
    <tbody valign=top>
    <?php
    echo "<tr>";
    
     echo '<td nowrap>"'.<a href="'.<?php the_permalink() ?>">.'<img src="'./wp-content/themes/sterafinOupo/images/i1.gif" width=3 height=16 border=0>'".'<?php the_author() ?>'</a><td colspan=17>
     echo "</tr>";
     }
     ?></table>

    I see the ‘”” are very important and ” because the enclosure balises with “” this echo “<td nowrap>”‘.“>.'<img src=”‘./wp-content/themes/sterafinOupo/images/i1.gif” width=3 height=16 border=0>'”.'<?php the_author() ?>”‘<td colspan=17> generate error T something. but if you use ‘ it work and with td or tr use “<td>” “</tr>”

    Fatal error: Call to undefined function: is_defined() in /home/.tica/gregjames/gregjames.org/wp-content/themes/gregblue/index.php on line 41

    Like I’ve said, I just want it under the FIRST post on the page, and ONLY that.

    Sorry, a stupid typo; instead of

    is_defined('AD_SHOWN')

    you should use

    defined('AD_SHOWN')

    Just erase is_; that should do the trick.

    Like I’ve said, I just want it under the FIRST post on the page, and ONLY that.

    That’s exactly what I was aiming for… :)

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘[Resolved] Putting an ad under just the FIRST post?’ is closed to new replies.
Skip to toolbar