I am currently trying to a create a feature on my category pages where on every 4th preview post, within the category an advert block is display.
essential it will work the following way:
post 1
post 2
post 3
post 4
ADVERT BLOCK
Post 5
Post 6
In my <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I have the following:
<?php $i = 0; ?>
<?php if (post_num ($i%4 == 0) < (5 - $featured_count)) : echo "this works"; endif ?>
can someone guide me in the right direction
Your more or less there, here's a quick example just showing some arbitrary text or the post title:
<?php
$counter = 0;
if (have_posts()) {
while (have_posts()) {
$counter++;
the_post();
if ($counter % 5 === 0) {
echo 'Advert Here!';
} else {
the_title();
}
}
}
?>
I haven't tested this, but just to give you an idea.
Edit: I should note that since you're looking to insert the advert after the fourth post, $counter % 4 won't do what you think, if you want to show four posts and then the advert, it would be placed in the 5th "position", hence $counter % 5.
Related
I am trying to figure out a way to print the sum of all the posts displayed in a loop. For example:
<?php if (have_posts()) :
$total_count = 1;
while (have_posts()) : the_post();
echo $total_count;
endwhile;
endif;
?>
Returns: 1 1 1 1 1 1 1
Since there are 7 posts in my loop. However, I would like to make a sum of all those 1's in order to get 7 as the result.
Any ideas?
Try to increment your total_count, instead of just printing it every time.
You will add 1 for each post and then print the total value. Check php assignment operators for +=
<?php if (have_posts()) :
$total_count = 0;
while (have_posts()) : the_post();
$total_count += 1;
endwhile;
echo $total_count;
endif;
?>
Also keep in mind your counter should always start at 0.
I am creating an infinite scrolling blog and would like ads to be shown every 5 posts. My code is as shown:
<?php
while (have_posts()) : the_post();
if ($i < 4) {
get_template_part( 'content', get_post_format() );
$i++;
} elseif ($i == 4) {
get_template_part( 'ad_template', get_post_format() );
$i=0;
}
endwhile;
?>
This works fine until Jetpack's infinite scrolling takes over and no longer shows the ads at all. I'm wondering if there is a way I can continue to show ads even though after 7 posts Jetpack takes the reigns.
Instead of having two different template parts, try adding that into the 'content' template.
Like so:
if ($i < 4) {
// Your actual post //
$i++;
} if($i == 5) {
// your ad code //
$i=0;
}
That way it should be a part the infinite scroll output. I have not tested this though so can not be 100%
I think I am pretty close to the solution but I cannot figure out the logic for where I should close the last div.
I have a wordpress query which requests four posts.
I want to wrap every two items in <section class="sponsor-row">
So I have implemented a counter in my query that counts each post, and if it finds two posts have been output it closes the <section> div off.
But I cannot work out how I should work out if the row should be closed again.
Can anyone figure this out? How can I make it wrap every two outputs in <section class="sponsor-row"> ?
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$counter = 0;
echo '<section class="sponsor-row">'; // Opening the section here
if ( has_post_thumbnail() ) {
$counter++;
echo '<div class="grid half">';
echo '<a class="sponsor" href="'.get_the_permalink().'" target="_blank">';
the_post_thumbnail( 'full' );
echo '</a>';
echo '</div>';
if ($counter <= 2){
echo '</section>'; // closing the section if two posts
$counter = 0;
}
}
}
}
Full query here: http://pastebin.com/e6RzZLR5
if you do if ($counter <= 2){ then it will close it each time it is less or equal 2, which means it will close it twice for each item. You should use if ($counter == 2){ and $counter = 0; should be at the top before the query, otherwise you will set it to 0 each loop.
See comparison in php
I'm currently counting the posts in the loop...
<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $count++; ?>
<?php include(locate_template('content.php')); ?>
<?php endwhile; ?>
But need to select specific posts programmatically in an if statement.
The post count I need to select is 1, 4, 5, 8, 9, 12, 13 etc (+3+1r) in sequence.
How can I select these posts (without having to manually type the numbers)?
While technically off topic for WordPress, I think this is something that a good number ueses will enquire about, particularly those that are new to both WordPress and PHP.
As suggested in the comments to your question, you can use a modulus operator to check this, and hopefully this answer will solve your problem.
<?php
$count = 0;
/* Start the Loop */
while ( have_posts() ) : the_post();
$count++;
if($count % 4 === 0 || $count % 4 === 1) :
locate_template('content.php', true);
endif;
endwhile;
?>
As a side note, the locate_template function will automatically load the template file with require_once (if you set the $load parameter), so you don't need to wrap it in include().
I'd recommended that you check if the template exists, and if not fall back on a theme default that will always be there.
My loop displays posts in two columns using this:
<?php
if (have_posts()): while (have_posts()) : the_post();
$count++;
?>
<?php if ($count == 1) : ?>
<div class="home-ci-row">
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div>
<div class="home-ci-gap"></div><!-- /* the gap */ -->
<?php elseif ($count == 2) : ?>
<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div> <!-- main-column-item-wrap -->
</div><!-- /* home-ci-row*/ -->
<?php $count = 0; ?>
<?php else : ?>
// No posts
<?php endif; endwhile; endif; ?>
You can see that the <div class="home-ci-row"> opens in the first count & closes in the second one </div>
so when my loop has an even number of posts works great, but with odd number it doesn't close the div
so My idea is this: If loop has even number
If loop has odd number of posts
By the way, you can do something like:
<?php
$count=0;
while(have_posts()){
if($count%2==0){
echo '<div class="home-ci-row">';
//draw your left div here
}else if($count%2==1){
//draw your gap here
//draw your right div here
echo '</div>';
}
$count++;
}
//close div if count is an odd number
if($count%2==1) echo '</div>';
?>
Is it possible to swap to a for loop? Is this what you need?
for ($i = 0; $i < $numberOfElements; $i++)
{
//if (odd number) && (this is the last element)
if (($i % 0 == 1) && ($i == $numberOfElements - 1))
{
//Special Case
}
else
{
//Normal Case
}
}
Caveat: Watch out for errors, PHP isn't my strongest language
This question was answered on WP Development StackExchange by fischi
Quoting from his answer:
You could do this much easier. As you are making a layout that can be achieved by floats, there is no need to declare a Row every second time.
In my Code example I just youse the $count to determine the Class of the HTML-Element. In combination with the total Number of Posts displayed.
If the total number of posts $wp_query->post_count is reached by the $count AND the total number is odd, I give the Element the Class fullwidth. In the same way, I determine if it is the first or the second (see the IF-Statement).
All I need to do afterwards is output the corresponding Class for each HTML-Element in the Loop. Besides the Class, no Element is diffferent from each other.
I use the Modulo Operator in PHP ( % ) to determine odd/even. It delivers 1 if I use $count % 2 and $count is odd. If you are not sure about this operator, read about it here.
So your code could look like this:
<?php
$count = 0;
if (have_posts()): while (have_posts()) : the_post();
if ( ++$count == $wp_query->post_count && ( $wp_query->post_count % 2 ) == 1 ) {
// if final count is reached AND final count is odd
// full width item
$postclass = "fullwidth";
$opentag = '';
$closingtag = '</div>';
} else if ( ( $count % 2 ) == 1 ) {
// if $count is odd it is the first item in a 'row'
$postclass = "halfwidth first";
$opentag = '<div class="home-ci-row">';
$closingtag = '';
} else {
// second item in a row
$postclass = "halfwidth second";
$opentag = '';
$closingtag = '</div>';
}
?>
<?php echo $opentag; ?>
<div class="main-column-item-wrap <?php echo $postclass; ?>">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div><!-- main-column-item-wrap -->
<?php echo $closingtag; ?>
<?php endwhile; endif; ?>