WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Need help modifying loop to display ads every few posts

[Resolved] Need help modifying loop to display ads every few posts

Viewing 15 replies - 1 through 15 (of 21 total)
  • Evan Herman
    Participant

    @eherman24

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

    mxpimp47
    Member

    @mxpimp47

    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.

    Christian1012
    Member

    @christian1012

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

    mxpimp47
    Member

    @mxpimp47

    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?

    Christian1012
    Member

    @christian1012

    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)

    mxpimp47
    Member

    @mxpimp47

    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.

    Christian1012
    Member

    @christian1012

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

    Christian1012
    Member

    @christian1012

    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

    mxpimp47
    Member

    @mxpimp47

    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.

    Christian1012
    Member

    @christian1012

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

    mxpimp47
    Member

    @mxpimp47

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

    Christian1012
    Member

    @christian1012

    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.

    mxpimp47
    Member

    @mxpimp47

    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

    esmi
    Forum Moderator

    @esmi

    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.

    mxpimp47
    Member

    @mxpimp47

    Thanks esmi – that is useful info for future issues!

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