Use WP_Query and then loop the results using break when you want to stop to put the ad.
$args = [
'posts_per_page' => 5
];
$posts = new WP_Query($args);
Then loop 2
<section class="posts">
<?php $cont = 0; ?>
<?php
while( $posts->have_posts() ) : $posts->the_post(); $cont++;
$id = get_the_ID();
//Show your post here
if($cont >= 1) break;
endwhile;
?>
</section>
<div class="ad">
<?php //Show ad ?>
</div>
<?php //Rest of the posts ?>
<section class="posts">
<?php
while( $posts->have_posts() ) : $posts->the_post();
$id = get_the_ID();
//Show your post here
endwhile;
?>
</section>
If you are doing this in a Post or Page you should reset the query after showing the posts:
wp_reset_postdata();
Related Posts:
- Getting only direct child pages in WordPress with get_pages
- Query multiple custom post types in single loop
- Inject post (from specific category) between posts in Loop
- Knowing the total number of posts before to get into the loop
- Build a content and excerpt grid loop with paging and options for # of posts
- Installing wp3.2.1 on IIS; getting empty sessions
- How to loop over custom fields in a page template?
- Want to separate sections of posts. Can you restart loop?
- Alter secondary loop to exclude posts from current page category
- Specific Loop For 2 Within Each
- Two posts in same div – WP loop
- Woocommerce, recognize the loop of related products
- Store loop into array
- Looping through WP_Post Object
- Easiest way to show total number of subpages
- Output 2 items within the Loop
- Show Custom Taxonomy Title
- How to exclude specific category from the get_the_category(); array
- spliting posts into two columns
- How to speed up a wordpress function with multiple loops?
- How can I add pagination and how can I change thumbnail size?
- How to Display a Single Post Excerpt
- $wpdb->flush(); breaks the loop
- Use object in template part
- Get first URL from post content
- Changing layout with wp_customise
- Show css depending on activity type in BuddyPress activity-loop [closed]
- Why my filterable portfolio page work not perfectly between slug button and slug output WORDRPESS?
- Iterate through posts based on array of categories
- get author_name from queried post
- How to add condition in wordpress loop? [closed]
- Alert Bar section within WP loop is displaying even though there are no posts
- Checking array against author id in loop
- How do I Turn This Into An if statement?
- Load wordpress content in other domain
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- Checkbox doesn’t stay checked, conten
- List posts that don’t have a specific tag?
- Can we count the WordPress Loop
- Add content after the first post in WP Loop
- Display an image if odd number of posts in grid
- How to loop through all the attached images in a post, and get their url one by one
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- 2nd page displaying the exact same posts as my first page (minus the very first post)
- Is the 404 page automatically displayed if a loop returns nothing?
- Last class on last headline?
- How to pass a variable to get_template_part that’s updated every time the template part is called?
- Issue with custom loop in Archive page
- use loop to return blog details
- home.php show blog posts as grid view
- How to add post meta in while loop?
- Echoing a CSS class based on category of post in a list
- Calling function in loop causes repeat data
- WordPress call post-ID in jquery
- How to Generate a list of Most Commented post?
- How to pick “full/thumbnail” images in the loop?
- How to Fix an Archive.php That Displays All Posts?
- List all anchor links on a page
- Php code error in wordpress if else statements [closed]
- How to add ASCII symbol after each recent post [closed]
- pagination broken – clicking next displays “page not found”
- Search.php gets metadata from first post
- the_post(); prints out style text into my HTML?
- How do I get pagination for get_posts() in WordPress or do I have to rewrite the whole code?
- Check if current post in loop is last on current page
- adding or removing endforeach; throws error!
- Adding PHP to an if else loop
- Accordion headings cuts off to second line on collapsing a accordion heading (used acf repeater)
- WordPress Recent Posts – Loop
- ACF background-color per post in a WordPress loop
- Date of last blog update for specific authors only?
- PHP for loop not working as intended
- Adding custom PHP to existing loop in Genesis
- How to Pass Current Taxonomy Terms into new WP_Query?
- For each 3 posts, show a different post type
- Loop returns more items than exist?
- Modify category archive page loop on functions.php
- WP_Post is not from correct array
- Post formating on Home page
- divide custom field values in div every two values
- WP Knowledge Base Theme bug – Subcategories and Articles, Need to change WP_Query
- Limit ads appearing more than three times
- Inserting A Feed and Sidebar into an HTML Page
- Pagination on Custom Loop
- What is the best way to get the first few post from WordPress in different divs using a loop?
- Transient Loop Not working as expected
- Advanced Taxonomy query, with 3 post types
- Custom single.php files for different post formats
- Troubleshooting “loopback requests that take too long”
- Why does a meta_query break this WP_Query?
- Conditional in foreach loop is outputting content twice
- Loop on a wordpress Page instead of content coming from the WP text editor
- How to create a loop where loop changes every post?
- Help with if/else loop [closed]
- $_html is empty when var dumped
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- Saving meta box data from selected option’s value in database is not working
- How to show correct td of table tags in wp_query loop
- fetch from an external api call and display results in page
- How could I prevent using the same custom loop in a template file when I only need to change one meta_query parameter?