Inserting ads into Jetpack infinite scroll loop - php

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%

Related

Get on every 4th post in WordPress loop

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.

Unlimited posts in custom layout PHP

Currently I am working on a Wordpress website with the posts showing in two columns running with a PHP code. The problem I am facing is that the posts showing on the website are limited to 10. Does anyone know where in the code I can make a change to show unlimited posts? Or do I need a whole new PHP code for this?
Would like to hear from you!
This is the code I am using
<?php $i = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
if($i == 0) {echo '<div class="ng-row">';}?>
<div class="half">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php $i++; if($i == 2) {$i = 0;echo '</div>';}?>
<?php endwhile; else: ?>
<?php if($i > 0) {echo '</div>';}?>
<?php endif; ?>
I've tried to change the numbers but it didn't work out well..
For an unlimited return set the posts_per_page to -1
add_action( 'pre_get_posts', 'set_posts_per_page' );
function set_posts_per_page( $query ) {
// This will set the query to return all results
$query->set( 'posts_per_page', -1 );
return $query;
}
Although be aware that this can be very bad for performance depending on the amount of posts. It should really only be done if you have a limited result set and know what that is.

Wrap outputs of a Wordpress query every second item

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

Selecting specific posts with an if statement in a counted loop

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.

Wordpress custom loop posts

I want to make a post page in the category.php template.
I want the first post to be a block of 600px by 600px and then 4 posts next to that of all 295px by 295px and the row under that first 4 blocks and than 1 big block etc.
So it will look like this:
can this be achieved with the
<?php while (have_posts()) : the_post(); ?>
loop?
Thanks.
Yeah, you can do this, but you'll want to look at get_posts instead. Check out the examples there, they'll give you something to work with.
Yes. You just need to keep track of whether the big image is on the left or right, and have a count running to tell you which post you're on in the grid/row...and then a way to close out the div tags when the loop is over. Something along these lines:
<?php
$big_left = true;
$items_displayed_in_this_row = 0;
while( have_posts() ){
if( $big_left ){
if( 0 == $items_displayed_in_this_row ){
// Code to put the big image on the left
the_post();
} else {
// Code to put the small images on the right
the_post();
}
} else {
if( 4 == $items_displayed_in_this_row ){
// Code to put the big image on the right
the_post();
} else {
// Code to put the small images on the left
the_post();
}
}
$items_displayed_in_this_row++;
if( 5 == $items_displayed_in_this_row ){
if( $big_left ){
$big_left = false;
} else {
$big_left = true;
}
$items_displayed_in_this_row = 0;
}
}
// While loop just ended. Close out whatever divs might be open.
// eg, if $items_displayed_in_this_row > 0, you're in the middle of a row
// and should close it.
?>
No matter what, it's going to be long and ugly. But doing it in the main loop like this instead of a custom query will allow you to take advantage of pagination, prev/next, etc.

Categories