Below is the code to display the post. That loads every 10 posts. And I want to display banner after 3 post. Can someone tell me, code to display banner after 3 post? By completing the code below. Thank you so much for your help.
<?php
$stories = Wo_GetPosts(array('limit' => 10));
if (count($stories) <= 0) {
echo Wo_LoadPage('story/no-stories');
} else {
foreach ($stories as $wo['story']) {
echo Wo_LoadPage('story/content');
}
}
?>
You are trying to use count()
in a way that this function is not intended. Per the PHP documentation:
Counts all elements in an array, or something in an object. http://php.net/manual/en/function.count.php
What you need to do is create a counter variable that increments within your foreach
loop, and when it hits 3 outputs the banner. Your code might look something like this:
<?php
$stories = Wo_GetPosts(array('limit' => 10));
// No stories; output error
if (count($stories) <= 0)
echo Wo_LoadPage('story/no-stories');
// Stories exist; show them!
} else {
$count = 0;
// Loop through $stories
foreach ($stories as $wo['story']) {
// Increment the value of $count by +1
$count++;
if ($count == 3) {
// Output my Banner here
}
echo Wo_LoadPage('story/content');
}
}
?>
One thing I would note is that the above code only outputs a banner one time; when the value of $count
is 3
. You could adjust this to run ever 3rd story by changing the match from if ($count ==3)
to if ($count % 3 == 0)
which essentially reads as If the value of $count is divisible by 3
.
I guess you create some HTML Code with this? If true, I'd use some Javascript for this purpose. Something like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="stories">
<div>story 1</div>
<div>story 2</div>
<div>story 3</div>
<div>story 4</div>
</div>
<script type="text/javascript">
$("#stories>div:nth-child(3)").after('<div class="banner">your bannery</div> ');
</script>
</body>
</html>