• web-girl

    (@web-girl)


    Is there a way to specify what the fifth post in the loop will be? I have the loop getting 20 posts, but I’d like every fifth post to be from the Category Ads? So, Post 1, Post 2, Post 3, Post 4, Ad Category Post #1, Post 6, Post 7, Post 8, Post 9, Ad Category Post #2, etc.? This way posts that are ads would intersperse with regular posts every five posts?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter web-girl

    (@web-girl)

    I’ve been trying to figure this out and I’m still not sure how to do it. How I’ve set it up so far is to have the main loop get all the posts, then a second loop get posts from the Ad category. I’m using this website’s tutorial to add the ads between the posts on the index page: https://www.howtostartablog.com/how-to-put-ads-in-between-my-posts.php

    I’ve replaced the part where I’d input the AdSense code with:

    <?php include(TEMPLATEPATH . '/includes/advertisements.php'); ?>

    To include the advertisements, and all this is working fine.

    However, I’m getting stuck on how to set up the query for the my advertisements include. So far I have this:

    <div class="entry small" style="padding-top:2px;height:310px;">
    
    <?php $my_query = new WP_Query( "cat=84" );
       if ( $my_query->have_posts() ) {
           while ( $my_query->have_posts() ) {
               $my_query->the_post();
               the_content();
           }
       }
       wp_reset_postdata();
    ?>
    </div><!-- end .entry -->

    This is outputting post content from posts in the Ad category, which is perfect. But, the problem is that I only want to get one ad per case statement and I don’t want to duplicate the add. Currently, the loop will get all posts in the Ad category and also will duplicate them.

    Or is this just the wrong way to set it up, because currently the ads only show on the index page, but eventually I also want them to show on category pages.

    Any help will be appreciated!

    dtrickky

    (@dtrickky)

    I think to achieve this you need to either run multiple loops, one which will loop the 4 most recent posts, followed by a second loop which runs a randomized post from the “Ad” category (and repeat)….Or construct a type of foreach loop that filters the 5th post to pull from the “Ad” category.

    I’m not sure what would be easier, but my hunch is the multiple loops.

    It would look like:
    Loop ->4 Most recent posts excluding “ad” category/end loop
    Loop -> Random Ad post /end loop
    Loop -> 4 Recent posts starting from the 5th post & excluding “ad” category /end loop
    Loop -> Random Ad Post /end loop
    Repeat til you are happy

    Someone else may know of a different way to make this work, but as long as you are resetting your queries, I think this should work with what you’ve described.

    Thread Starter web-girl

    (@web-girl)

    Thanks for your help – I will have to try this out and see if it works. I’m sorry, I’m still not very familiar with queries, but how would I start the 3rd Loop from where the first one left off (i.e., at the 5th post)?

    dtrickky

    (@dtrickky)

    I believe you just use the “offset” parameter.

    <?php query_posts ($query_string . '&cat=1&showposts=3&offset=1&orderby=date'); ?>

    This would query posts from the category ID of 1, show 3 posts, start from the 2nd post (offset=1), and order by their date.

    Make sense?

    Thread Starter web-girl

    (@web-girl)

    Thanks, I used the code and now I have ads appearing every five posts!!

    I wish there was a way to automate it, though. Even though the index page only shows 20 posts, if I were to go look at previous pages of posts, the ads would disappear. So if there were 100 older posts, for example, I’d have to write code in increments of 5 up to 100 to get the ads to appear? That’s assuming that a bunch of ads are running.

    Also, I’m also not sure how to change the index page query to take into account dynamically the number of ads showing in relation to posts? Now it shows 20 posts and then 4 ads, a total of 24. I don’t know if there is a way to dynamically adjust this depending on how many posts are being displayed in total.

    The problem is that I’m trying to help someone with their WP, so I’m not sure how a lot of these things might work over time.

    Thanks for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How Do I Specify the Fifth Post in the Loop to be a Post from a Certain Category’ is closed to new replies.