Sorry, can you please describe a little more detail about what you are trying to achieve? You want to insert an ad every X amount of posts? You want to show an ad where?
Thanks
Chris
I want to run basically my category posts loop, like 50 for example on category page. And every 6 posts i want to be able to add the Ad. something like that:
X X X
X X X
—–ad
X X X
X X X
—–ad
X X X
X X X
—–ad
X X X
X X X
X X X
X X X
maybe the widget placeholder
-
This reply was modified 8 years, 6 months ago by
aosipov.
I tried this way:
get_header(); ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="site-main-for-travel">
<?php
echo '<h2 class="display page-title">';single_cat_title();echo '</h2>';
the_archive_description( '<div class="archive-description">', '</div>' );
?>
<div class="row">
<?php $current_cat = get_the_category();
$cat_ID = $current_cat[0]->cat_ID;
$loop = new WP_Query(array(
'cat' => $cat_ID,
'post_type' => 'post',
'posts_per_page' => 2,//posts per page
'orderby' => 'date',
'order' => 'DESC'
));?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-6'); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a class="post-teaser" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="thumbnail-block"><?php the_post_thumbnail('regular-post-thumbnail'); ?></div>
<div class="title-block"><span class="h3 title-for-widget"><?php the_title(); ?></span></div>
</a>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>
</div>
<div>Ad-2</div>
<div class="row">
<?php $current_cat = get_the_category();
$cat_ID = $current_cat[0]->cat_ID;
$loop = new WP_Query(array(
'offset' => 2, //Set your offset
'cat' => $cat_ID,
'post_type' => 'post',
'posts_per_page' => 6,//posts per page
));?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4'); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a class="post-teaser" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="thumbnail-block"><?php the_post_thumbnail('regular-post-thumbnail'); ?></div>
<div class="title-block"><span class="h3 title-for-widget"><?php the_title(); ?></span></div>
</a>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>
</div>
<div>Ad-3</div>
<div class="row">
<?php $current_cat = get_the_category();
$cat_ID = $current_cat[0]->cat_ID;
$loop = new WP_Query(array(
'offset' => 8, //Set your offset
'cat' => $cat_ID,
'post_type' => 'post',
'posts_per_page' => 6,//posts per page
));?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4'); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a class="post-teaser" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="thumbnail-block"><?php the_post_thumbnail('regular-post-thumbnail'); ?></div>
<div class="title-block"><span class="h3 title-for-widget"><?php the_title(); ?></span></div>
</a>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>
</div>
<div>Ad-4</div>
<div class="row">
<?php $current_cat = get_the_category();
$cat_ID = $current_cat[0]->cat_ID;
$loop = new WP_Query(array(
'offset' => 14, //Set your offset
'cat' => $cat_ID,
'post_type' => 'post',
'posts_per_page' => 6,//posts per page
));?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4'); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a class="post-teaser" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="thumbnail-block"><?php the_post_thumbnail('regular-post-thumbnail'); ?></div>
<div class="title-block"><span class="h3 title-for-widget"><?php the_title(); ?></span></div>
</a>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
but it doesn’t work for some categories, just shows random categories, i guess it doesn’t do reset and i have no idea why:(
-
This reply was modified 8 years, 6 months ago by
aosipov.
Have you thought about something like this where you just run a simple loop counter?
We set var $loop_counter to 0 and every time the loop goes through a new post it increments $loop_counter by 1. When it reaches 5, it displays an ad and resets the counter back to 0. To display the ad we just organize them into a directory in your themes folder called “ads” and then each ad file would be called ad-1.php, ad-2.php, ad-3.php etc…
Then we just call one of the ads randomly like this:
get_template_part(‘ads/ad-‘ . rand(1,5))
I updated your code below to show what I mean, it is untested, you may need to make changes. Also, if you don’t want random ads, you could just put a second counter that counts how many times an ad was shown which would give you a sequence in order.
get_header(); ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="site-main-for-travel">
<?php
echo '<h2 class="display page-title">';single_cat_title();echo '</h2>';
the_archive_description( '<div class="archive-description">', '</div>' );
?>
<div class="row">
<?php $current_cat = get_the_category();
$cat_ID = $current_cat[0]->cat_ID;
$loop_counter = 0;
$loop = new WP_Query(array(
'cat' => $cat_ID,
'post_type' => 'post',
'posts_per_page' => 50,//posts per page
'orderby' => 'date',
'order' => 'DESC'
));?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
if($loop_counter >= 5){
get_template_part('ads/ad-' . rand(1,5));
$loop_counter = 0;
} else {
$loop_counter++;
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-6'); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<a class="post-teaser" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="thumbnail-block"><?php the_post_thumbnail('regular-post-thumbnail'); ?></div>
<div class="title-block"><span class="h3 title-for-widget"><?php the_title(); ?></span></div>
</a>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
Thank you ckbrewer15, i will try that approach, but what if i want to display 2 first posts differently, and continue as a regular loop after that
You just need to rearrange ckbrewer15’s code a bit, including the two display code styles within the if $loop_counter logic.
if $loop_counter >= 2 then display a regular loop (do not reset the loop counter)
else display posts differently (the first two posts when the counter is 0 or 1)
Thank you @bcworkz, that helps!