-1

I am using this code for showing ads after one post in the home page. It's working fine. But I want to show an ad after one post and another one ads after 5 post. What will the loop be. Any suggestions?

  <?php if((have_posts)): ?>
  <?php $count=0; ?>
  <?php while(have_posts()): the_post(); ?>
  <?php $count   ?>
  <?php if($count==2): ?>
     <div>Ads code here</div>
     <h2><a href="<?php the_permalink(); ? >" title="<?php the_title(); ?>"><?php.  the_title(); ?></a></h2>
     <?php else: ?>
     <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
  <?php endif; ?>
  <?php endwhile; ?>
  <?php endif; ?> 
  • Reformat your code properly, your code is not visible. Please use code block in the editor for adding code, that is the {} symbol – Pieter Goosen May 10 '14 at 9:58
  • I edited your question to properly format your code. Seemed you misunderstood me. When you add code in the editor, you need to highlight your code and then on top of the editor is a symbol {}. You need to press that. You will then see in the preview block that your code shows up as it does now. – Pieter Goosen May 10 '14 at 10:18
0

I don't believe the code worked what you have posted. If you simply copied and pasted it from your template, it would never have worked.

There are a couple of syntax errors/bugs in your code

  1. <?php if((have_posts)): ?> should be <?php if(have_posts()): ?>.

  2. <?php $count ?> What does this mean. This is suppose to update your timer, but as it stand it has no function at all. One of the reasons I'm sure nothing worked. This should be <?php $count++ ?>

  3. ? > in <?php the_permalink(); ? > is a syntax error. Never lave a space between ? and >. Another reason why your code could not work.

  4. <?php. in <?php. the_title(); ?> Don't add anything directly after the php tag, always leave a space. In this instance it might just be a typing error as the full stop has no meaning in that particular code, but it is bug

  5. You can simply just add a or and add a new counter to your code to display your ads after post 1 and 5. <?php if( $count==2 || $count==6 ): ?>

So your bug free and working code should look something like this

<?php if(have_posts()): ?>
    <?php $count=0; ?>
    <?php while( have_posts() ): the_post(); ?>
        <?php $count++ ?>
        <?php if( $count==2 || $count==6 ): ?>
            <div>Ads code here</div>
            <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
                <?php else: ?>
            <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?> 

EDIT

I forgot to add this. You should always when developing a theme/plugin, set your debug to true in wp-config.php. If you have done that, you would have been able to identify all of these bugs and rectify them before you have added your code to your question

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.