WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Putting an ad under just the FIRST post? (19 posts)

  1. jmlares
    Member
    Posted 6 years ago #

    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?

  2. NC@WP
    Member
    Posted 6 years ago #

    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; ?>
  3. jmlares
    Member
    Posted 6 years ago #

    How would this work though?

  4. NC@WP
    Member
    Posted 6 years ago #

    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.

  5. jmlares
    Member
    Posted 6 years ago #

    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.

  6. NC@WP
    Member
    Posted 6 years ago #

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

  7. jmlares
    Member
    Posted 6 years ago #

    And where do I stick the ad's HTML?

  8. Chris_K
    Member
    Posted 6 years ago #

    right where it currently says:

    //show ad

  9. NC@WP
    Member
    Posted 6 years ago #

    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!

  10. jmlares
    Member
    Posted 6 years ago #

    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(); ?>
  11. NC@WP
    Member
    Posted 6 years ago #

    So where do you want the ad?

  12. NC@WP
    Member
    Posted 6 years ago #

    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(); ?>
  13. NC@WP
    Member
    Posted 6 years ago #

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

  14. Baudry
    Member
    Posted 6 years ago #

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

  15. jmlares
    Member
    Posted 6 years ago #

    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.

  16. NC@WP
    Member
    Posted 6 years ago #

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

  17. Anonymous
    Unregistered
    Posted 6 years ago #

    Hello NC@WP, just what I was looking for, thank you.

    Just one question;

    When I put this in my page twice it stops the second ad from displaying altogether, logged in or out the second ad does not show.

  18. NC@WP
    Member
    Posted 6 years ago #

    Um, of course... You need to change the code to reflect your intent to show the second ad...

  19. shiola
    Member
    Posted 5 years ago #

    How would I do this without the logging in feature? I want to show ads to everyone - mainly me, cause if I'm logged in I'll forget about them in a months time :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.