Support » Fixing WordPress » Need help modifying loop to display ads every few posts

Viewing 15 replies - 1 through 15 (of 21 total)
  • <?php $count = 0; ?>
            if ( have_posts() ) : while ( have_posts() ) : the_post();
            global $post;
            <?php $count++; ?>
      <?php if ($count == 2) : ?>
              <div class="ad"><?php echo adrotate_group(1); ?></div>
            $more = 0;       // Set (inside the loop) to display content above the more tag.

    Shouldn’t you be adding 1 to $count at the end of the loop, not right at the beginning? This won’t fix it, just something I noticed immediately. It could also not really matter…

    man, I dont know I am just trying to figure this out. I am not an expert with php so I dont understand it enough to modify it correctly. Im just trying to plug and play the feature from reading up on it.

    The code you posted in pastebin is correct except for that you have to define $postnum before you loop through each result, so that the original value of $postnum can be incremented each iteration.

    	$postnum = 0; // Set counter to 0 outside the loop
    	if (have_posts()) : while (have_posts()) : the_post();
    	// Do regular stuff
    		$postnum++; // Increment counter
    		if ($postnum % 5 == 0){ // If the remainder of the counter value divided by 5 equals zero
    			// Do special stuff

    Thanks for the reply –

    I tried adding what you said but its not working. Could you create an updated pastebin of my template so I can see exactly what your saying?

    I looked again. I’m not sure if you pasted Evan’s suggestion, but if that was the case, he forgot an opening php tag which is probably an issue.

    Any way, here you are:


    Thanks for the pastebin – I applied that code and its not working you can see a *test site setup with dummy content here –

    I have that template applied to the “home” page. I cant even see the div “ad” show in the source code. Im so puzzled on this.

    Thanks again for the help on this.

    Christian Chung


    @mxpimp : I’m sorry to hear it’s not working for you. Ensure that you’re working on the right template. Add $count to the container ID in content-blog.php, and see if it increments properly.

    Christian Chung


    Here is the basic concept and it can be used anytime a loop iterates over an array. I hope this helps you understand how to solve the problem on your own.



    I get the phpfiddle you posted. Working with this theme is very confusing to me.

    here is the template content-blog.php –

    Whats weird is I put what we have been working on in the homepage.php template with a template name, and selected that template to be used on the “Home” page within wordpress. So in theory it should be using that template, right?

    What am I missing here?

    I have been picking my brain since your last post. I appreciate your help.

    Christian Chung


    Is there a file front-page.php? That will override any other template.



    No, there is a template named “homepage” that should override anything when chosen on the page admin under “page attributes”. That usually works. But that page’s loop calls a template that runs the actual loop that was in the pastebin link above – content-blog.php

    You saw that part of code get_template_part('content','blog'); in the code you corrected for me.

    So maybe thats where the $count needs to be. But its far from a simple loop for me to figure out the placement. If it were a standard loop like from twenty twelve it would be much easier. Thats why I needed assistance from a wizard such as your self ;).

    Christian Chung


    Okay, let’s do this first. Let’s find out what template you’re using for sure. That way, if nothing else, we know for sure where to start.

    First create a function that will echo/return the name of the template. Paste this into your functions.php file:

    // Identify and Define the Template Used to Render Page/Post
    function mx_define_template($template){
    	$GLOBALS['current_theme_template'] = basename($template);
    	return $template;
    // Make Global
    add_action('template_include', 'mx_define_template');
    // Function to display template name on the frontend
    function  mx_the_template($echo = false){
    	if (!isset($GLOBALS['current_theme_template'])){
    		exit ('Error: No Template Found');  // If nothing found, exit with error message
    	if ($echo){
    		echo $GLOBALS['current_theme_template'];  // To display on frontend, set param to true
    		return $GLOBALS['current_theme_template'];  // Default: Return

    Then display this on the top of every page. So open header.php and directly under the <body> tag, let’s create a new div. Paste this:

    <div> <?php mx_the_template(true); ?> </div>

    Note: This is helpful for dev sites, but not recommended for live or production sites.

    Once you do this, either leave it up for me to see, or post the result here. This will show us definitively what template is being used. Then we can trace the issue from there.



    Ahhh it is showing the index.php template is being used! I wonder how its over riding the selected template chosen on the page attributes – hmmm.

    here is the index.php template pastebin –



    Forum Moderator

    By design, the page you select in Reading -> Settings uses your theme’s index.php template file automatically and not any page template that you apply to the page.



    Thanks esmi – that is useful info for future issues!

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Need help modifying loop to display ads every few posts’ is closed to new replies.