Have an ad space repeat every 10 posts [duplicate] - php

This question already has answers here:
How to perform an action every 5 results?
(6 answers)
Closed 8 years ago.
I would like to put an adsense square (250x250) ad every ten posts in my posts list.
I tried adding this code to my index.php, inside of the posts div:
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 11) : ?>
Ad code is here
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
It showed up correctly, but all the format of the posts changed for some reason.
Plus, this code only puts an ad after the tenth post, and it doesn't automatically repeat it after the twentieth, etc.
This is not actually really important, I could just repeat the numbers.

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
//////////////////////
<?php if($count%10==0){ echo "</br></br></br>"; } ?>
///////////////////
<?php if ($count == 11) : ?>
Ad code is here
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php else : ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

Related

How do I get the link of a current WordPress post while it is in the Loop?

Below is my loop:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_post_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
Get <?php get_post_permalink(); ?> should display the link yet this is what is being rendered. It is not displaying the permalink of the post
None of the other answers are correct. get_the_permalink() (you can use get_permalink() as well, since it's an alias) RETURNS the data, not ECHO. So, it will never be printed to the screen (most WP functions with get_ prefix work this way.)
You have two options:
Use get_permalink( get_the_ID() ) pass the current post id (if not in the loop) and echo it.
Use the_permalink() which will echo out the permalink (in the loop);
the_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php the_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
get_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php echo get_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
This will echo out the URL, but will not make the link clickable - you need to add it to an <a> tag:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
Click Here
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
If you want to get Post Permalink you should use get_permalink($post_id)
Wordpress get_permalink function Reference
Please try this:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post();
$id = get_the_ID();
?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_the_permalink($id); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>

PHP Code to show search results on a seperate Page

I tried for a while now to find some code on the INternet which I can put inside my search.php file which i created to show all search results on that specific page and not on the default wordpress search site, but didn´t found anything that fits my problem.
I have tried several codes withing my search.php file but they either crashed my site or didn´t worked the way i need it
<?php get_header(); ?>
<?php echo do_shortcode('[et_pb_section global_module="4027"][/et_pb_section]'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="searchResults">
<a href="<?php the_permalink() ?>">
<h2><?php the_title(); ?></h2>
</a>
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php _e( 'Nothing Found' ); ?>
<?php endif; ?>
<?php
get_footer();
This is some of what I have tried: (taken from comments)
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="searchResults">
<a href="<?php the_permalink() ?>">
<h2><?php the_title(); ?></h2></a>
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php _e( 'Nothing Found' ); ?>
<?php endif; ?>

how to display odd number post in left side even number post in right side?

<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>

Wordpress: Most recent post in one column, other posts in two columns

My client is looking to change her homepage to look similar to this site: http://www.eatliverun.com/
Where the most recent post is up top in full in one column and the others are in two column excerpts.
She wants to have just one recent post display and the rest in the two column format. I'm just not sure how to accomplish this correctly.
Her website is located at http://pamelastable.com/
Integrating a counter into your loop can easily help you achieve this layout. Try something as follows (note: the 'six columns' class represents your 2 column layout in a 12 column grid):
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ($count == 1) { ?>
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
<?php $count++; ?>
<?php } else { ?>
<?php if($count % 2 == 0) { ?>
<div>
<? } ?>
<div class="six columns">
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
</div>
<?php if($count % 2 == 1) { ?>
</div>
<? } ?>
<?php $count++; ?>
<? } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Just add a counter to the loop and style the html differently for the first iteration.
<?php $i = 0; while ( have_posts() ) : the_post(); //inside loop ?>
<?php if ($i++ == 0) : ?>
<div>First Post</div>
<?php else: ?>
<div>Other Posts</div>
<?php endif; ?>
<?php endwhile; ?>
In your index.php or other template file^
Just use a counter to see which post number you are on.
<?php
if (have_posts()) {
$i = 0;
while (have_posts()) {
the_post();
if (++$i == 1) {
echo '<div class="entry-large">';
} else {
echo '<div class="entry-small">';
}
the_content();
echo '</div>'
}
}

Alternate markup for every 6 post - in wordpress

I want to put in and ad code after every 6 post on my blog. I cant really figure out how to break out of the foreach and insert the ad code.
This link will help you. Third title says: Insert Ads After The First Post
Change the code for 6 where it says 2:
<?php if (have_posts()) : ?> // Here we check if there are posts
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?> // While we have posts, add 1 to count
<?php if ($count == 6) : ?> // If this is post 6
//Paste your ad code here
<h2><?php the_title(); ?></h2> // post title
<?php the_excerpt(); ?> // You may use the_content too
<?php else : ?> // If this is not post 6
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
UPDATE: As Gordon noticed, you asked for code every 6 posts (sorry I missed that on my first read). So the code should be:
<?php if ($count % 6 == 0) : ?>
As what #Gordon commented, here's how I'd re-factor that code;
<?php if (have_posts()) : $count = 1; while (have_posts()): ?>
<?php if ($count == 6) : ?>
// Paste your ad code here
<?php $count = 0; endif; ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php $count++; endwhile; endif; ?>

Categories