WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help modifying loop to display ads every few posts (22 posts)

  1. mxpimp47
    Member
    Posted 1 year ago #

    I have found some old threads on here but my loop in the theme I am using is a bit tricky. I have attached a complete template page in the pastebin link. You can see the "ad" div at the bottom before the endwhile statement that has the php code for echoing the adrotate banner.
    *edit - posted wrong pastebin link
    http://pastebin.com/jbpcQceS

    here are the other links I have found that I couldnt get to work for me, but may help some others.
    http://wordpress.org/support/topic/inserting-ads-between-every-5th-post
    http://wordpress.org/support/topic/the-loop-place-an-ad-every-n-posts
    http://wp.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/

  2. Evan Herman
    Member
    Posted 1 year ago #

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

  3. mxpimp47
    Member
    Posted 1 year ago #

    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.

  4. Christian1012
    Member
    Posted 1 year ago #

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

    <?php
    	$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
    		}
    	endwhile;endif;
    ?>
  5. mxpimp47
    Member
    Posted 1 year ago #

    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?

  6. Christian1012
    Member
    Posted 1 year ago #

    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:
    http://pastebin.com/pAcTmzz6

    (untested)

  7. mxpimp47
    Member
    Posted 1 year ago #

    Thanks for the pastebin - I applied that code and its not working you can see a *test site setup with dummy content here - http://insider.claytonmiller.com/home/

    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.

  8. Christian1012
    Member
    Posted 1 year ago #

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

  9. Christian1012
    Member
    Posted 1 year ago #

    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.

    http://phpfiddle.org/main/code/6342312

  10. mxpimp47
    Member
    Posted 1 year ago #

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

    here is the template content-blog.php - http://pastebin.com/2EcZxcE7

    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.

  11. Christian1012
    Member
    Posted 1 year ago #

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

  12. mxpimp47
    Member
    Posted 1 year ago #

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

  13. Christian1012
    Member
    Posted 1 year ago #

    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
    	}
    	else{
    		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.

  14. mxpimp47
    Member
    Posted 1 year ago #

    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 - http://pastebin.com/K4ftRnmE

  15. esmi
    Forum Moderator
    Posted 1 year ago #

    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.

  16. mxpimp47
    Member
    Posted 1 year ago #

    Thanks esmi - that is useful info for future issues!

  17. Christian1012
    Member
    Posted 1 year ago #

    @esmi: Amen.

    @mxpimp:
    It would seem as if you have everything you need now. It looks like you even got it figured out yourself. Good job. I would recommend rolling the homepage template we were editing earlier back to the original to avoid future issues. The pastebin on the original post was and still is, a complete mess.

    If I could give you one more suggestion. Now that we have that working properly, you don't want to write out all the code to display the ads on index.php. Use get_template_part(). More info here.

  18. mxpimp47
    Member
    Posted 1 year ago #

    ok i got it working. but I need just a little help cleaning up how this part is written -

    /* Set up and display AdRotate content */
                    if ($count % 5 == 0){                   // Returns True every 5th post
                            echo '<div class="boxed-mason col2"><div class="content-container">';
                            echo adrotate_ad(1);             // Displays Ads on Frontend
                            echo '</div></div>';
                    }

    its not wrapping those two divs around the adrotate correctly and its leaving theme open and breaking the layout. And the other post are inside of the opening divs. you can view the page an see what I am talking about.

    Thanks for helping me with this I really appreciate this a lot.

  19. Christian1012
    Member
    Posted 1 year ago #

    Is ad rotate outputting an extra </div>? Comment out // echo adrotate_ad(1); and add </p>Hello World</p> below it on a new line. If that works okay, the issue is with adrotate_ad() function, which should be supported by the original developer.

    And for readability, change this line:

    echo '</div></div>';

    to this (updated, my bad.):

    echo '</div>' .PHP_EOL;
    echo '<!-- end content-container -->' .PHP_EOL;
    echo '</div>' .PHP_EOL;
    echo '<!-- close boxed-mason-->' .PHP_EOL;
  20. mxpimp47
    Member
    Posted 1 year ago #

    Edit- the way I had it worked too as far as my previous post once I resolved whats below. But for better readability is it more appropriate to have it the last way you posted?

    After digging into it further it was within the adrotate ad settings, not an actual problem. Some how the closing tag for the url was left off and breaking the layout. But its up and running now.

    Thanks so much for the help with this. I appreciate it.

    One last question - any resources you recommend reading up on to help bridge the gap between wordpress and standard php usage? It takes a bit to figure out things or outside help or clarification when working with wordpress. I find standard php examples all the time. But sometimes I cant apply them cause I don't know how to tweak it to work with wordpress functions etc...

    Again thanks for the help!

  21. Christian1012
    Member
    Posted 1 year ago #

    No the readability thing isn't necessary or anything, but I think adding a comment when you close a container div is sort of best practices. It's really for other devs/programmers, so when they read your code, they can see what's going on quickly and easily.

    As for resources, there are a lot, but none specific to

    help bridge the gap between wordpress and standard php usage.

    However, I will say this. There is nothing special about WordPress' PHP.

  22. mxpimp47
    Member
    Posted 1 year ago #

    Thanks. I just know that sometimes with all the necessary hooks for functions its is hard for me to wrap my head around sometimes.

    Thanks again for the help. The community has always got me through a hang up!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags