2

I'm working on my first ever WP project.

I have a theme that tiles all posts across the homepage - and I need to add a repeating advert every 5-6 posts.

My thought was to change the database query that lists the posts and add the advert every so many loops.

Can someone point me in the direction of where to find the database query?

Or is there a more elegant solution you could suggest?

thank you.

  • you could try to add an action to the 'the_post' hook; then use a conditional based on the $wp_query->current_post counter to show the ad...details depend on what theme you are using. – Michael Apr 6 '18 at 17:28
  • @Michael yes, that approach can make the injection theme independent of course – Andy Macaulay-Brook Apr 6 '18 at 17:50
  • @AndyMacaulay-Brook - not fully theme independant, as in this case the grid system is possibly based on CSS selectors; so to fit the ads into the grid, they need to be wrapped into the same HTML/CSS structure. at least, you won't need to identify the exact theme file to edit the loop of the home page. – Michael Apr 6 '18 at 20:05
  • @Michael I think he meant dependent, but yeah, the_post() could be in some counterintuitive places. There's no rule that it has to be immediately after the have_posts(). The post's wrapping element could be already open. – Jacob Peattie Apr 7 '18 at 13:34
2

Probably just add a counter, show ad on multiples of 6.

Something like

$count = 0;
$adEvery = 6;

if (have_posts()) :
    while (have_posts()) : the_post();

        // Individual Post

        $count++;
        if ($count%$adEvery == 0) { 
            // your ad
        } 
    endwhile;
else :
    // No Posts Found
endif;
  • thank you - yes, that's what I was thinking - but could you tell me which file contains this call? I'm not familiar to the WP file structure at all. – Stuart Hall Apr 6 '18 at 17:14
  • browse your theme directory. specific file depends on where you want it, home, archives, category, etc, etc. checkout developer.wordpress.org/themes/basics/template-hierarchy – David Sword Apr 6 '18 at 17:16
1

Phil Kurth has written an informative article on split handling the WordPress loop using the current_post property within the global $wp_query object.

This can be applied to your question, and allows us to cleanly insert content at any point in a loop.

The function is as follows (place in functions.php or, as I prefer, put in a seperate library file that handles query mods only):

/**
 * Returns the index of the current loop iteration.
 *
 * @return int
 */
function pdk_get_current_loop_index() {
    global $wp_query;
    return $wp_query->current_post + 1;
}

Then when outputting the loop, if we want to inject the ad after the 6th post:

if ( have_posts() ) :
    while (
        have_posts() ) :
        the_post();

        get_template_part( 'content' );

        if ( pdk_get_current_loop_index() === 6 ) {
        ?>
            <div class="ad-mrec">
                <!-- Insert ad coder here -->
            </div>
        <?php
        }

    endwhile;
endif;

If you read Phil's article, there's also more you can do with this function.

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.