It should be a slider, which shows all posts, but only 15 posts on each slide. I get all Posts.
(I use Wordpress functions.)
In the -div class = "slide"- there are 15 posts, after that a new -div class = "slide"- with 15 posts should be created.
Here is the code for all posts:
$myposts = get_posts($args);
$result = '<div id="fullpage">';
$result .= '<div class="section" id="section1">';
$result .= '<div class="slide">';
foreach ($myposts as $post) {
$result .= '' . $post->post_title . '';
// the_post_thumbnail('full');
}
$result .= '</div>';
$result .= ' </div>';
$result .= '</div>';
return $result;
After 15 posts, I would like a new slide. I do not know how to adjust the foreach loop. Should I do this with an if statement, or can I do this with a foreach loop?
Use a counter, and reset it after 15. (Not tested, I have no PHP at the moment)
$counter = 1;
foreach ($myposts as $post) {
if ($counter == 1) {
$result .= '<div class="slide">';
}
$result .= '' . $post->post_title . '';
$counter++;
if ($counter == 16) {
$counter = 1;
$result .= '</div>';
}
}
Related
I have the below function which gets wordpress posts and outputs them in a bootstrap grid (3 posts per row). Each array chunk has 3 posts in it. Basically what I want to do is if the chunk is not complete and only has say 2 posts in it then the first post in that chunk gets "col-sm-offset-2" class added. I believe I need some way off counting the posts in the chunk but I'm not sure how to implement.
function post_events($atts) {
global $post;
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
);
$posts = get_posts($args);
$posts_chunks = array_chunk($posts, 3);
$output = '';
foreach ($posts_chunks as $row) {
$output .= '<div class="row">';
foreach ($row as $post) {
setup_postdata($post);
$output .= '<div class="col-md-6 col-sm-6 event-item">';
$output .= '' .get_the_post_thumbnail(). '';
$output .= '<div class="event-item-text">';
$output .= '<h3>' .get_the_title(). '</h3>';
$output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
$output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
return $output;
}
add_shortcode('post_events','post_events');
You can just use count() on $row, and then set the class based on whether it's the first iteration or not:
$class = 'col-md-6 col-sm-6 event-item';
$count = count($row);
foreach( $row as $k => $post )
{
$cls = ($k == 0 && $count < 2) ? $class.' col-sm-offset-2' : $class;
setup_postdata($post);
$output.= '<div class="'.$cls.'">';
//...
}
Since you have chunked, the subarrays will be 0 based and the first post will be 0. So just check that and if the count is less than 3:
foreach ($posts_chunks as $row) {
$output .= '<div class="row">';
foreach ($row as $key => $post) {
$extra = ''; //set extra class to empty
if($key == 0 && count($row) < 3) { //check first post and count less than 3
$extra = 'col-sm-offset-2 '; //set extra class
}
setup_postdata($post);
//use $extra somewhere
$output .= '<div class="'.$extra.'col-md-6 col-sm-6 event-item">';
$output .= '' .get_the_post_thumbnail(). '';
$output .= '<div class="event-item-text">';
$output .= '<h3>' .get_the_title(). '</h3>';
$output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
$output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
I've been trying to read up on this in the manual, but basically I've got an array where I'm trying to reverse it and exclude the last item. So I got 14 items currently in my array and I'm getting it to reverse and it shows 14-2. My code got it to exclude the last item. So I guess it technically works, but I actually want it to output as 13-1. I tried using array_pop and array_shift but I didn't know how to integrate it with array_reverse.
function smile_gallery( $atts ) {
if( have_rows('smile_gallery', 3045) ):
$i = 1;
$html_out = '';
$html_out .= '<div class="smile-container">';
$html_out .= '<div class="fg-row row">';
// vars
$rows = get_field('smile_gallery', 3045);
$count = count( get_field('smile_gallery', 3045) );
$html_out .= '<div class="col-md-8">'; // col-md-offset-1
$html_out .= '<div class="smile-thumbs">';
foreach( array_reverse($rows) as $row) :
// vars
$week = "smile_week";
$img = "smile_img";
$caption = "smile_caption";
// Do stuff with each post here
if( $i < $count) :
$html_out .= '<div class="smile-thumb-container">';
$html_out .= '><h6>Week ' . $row["smile_week"] . '</h6></a>'; // smile thumb week
$html_out .= '</div>'; // smile thumb container
endif;
$i++;
endforeach;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>'; // smile container
endif;
return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
I'm reading your question as the following, correct me if I'm wrong.
I've got an array where I'm trying to reverse it and exclude the first and last items.
To do that as you know you're want to use array_pop() and array_shift().
<?php
//
$rows = get_field('smile_gallery', 3045);
$count = count($rows);
array_pop($rows);
array_shift($rows);
foreach (array_reverse($rows) as $row):
...
If you want to reverse first and then do your operations, which is not required if your removing items from both ends. Take out array_reverse from the foreach and then do your manipulations.
<?php
// vars
$rows = get_field('smile_gallery', 3045);
$count = count($rows);
$rows = array_reverse($rows);
array_pop($rows);
array_shift($rows);
foreach ($rows as $row):
...
Let me know if that helps.
I have an array which can have any no. of elements in it. Now i want to loop this array and create design such that each li can have 15 elements inside it , next set of li will be created based of multiples of 15's elements.
Now my array has exact 15 elements and the code i am trying creating 2 li , which it should create only 1 li.
May be my logic is too bad or I am missing anything.
Here is my code:-
<?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); ?>
<div class="slide">
<?php $design = '<ul class="slides"><li><div class="MainSlider">';
foreach($result as $key=>$row)
{
$design .= '<div class="MainSliderPic">'.$key.'</div>';
if(($key+1)% 15 == 0){
$design .= '</div></li><li><div class="MainSlider">';
}
if(count($result) == $key+1){
$design .= '</div></li></ul>';
}
}
echo $design;
?>
</div>
You can use array_chunk for to achieve it:
$result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$chunks = array_chunk($result, 15);
foreach ($chunks as $chunk) {
echo '<ul><li>';
echo implode('</li><li>', $chunk);
echo '</li></ul>';
}
Don't mix opening and closing of tags in your code. Do it separately where it belongs, e.g.
$design = '<ul class="slides">';
$n = 0;
foreach($result as $key=>$row) {
if ($n == 0)
$design .= '<li><div class="MainSlider">';
$design .= '<div class="MainSliderPic">' . $key . '</div>';
++$n;
if ($n == 15) {
$design .= '</div></li>';
$n = 0;
}
}
$design .= '</ul>';
echo $design;
Try following code:
<?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); ?>
<div class="slide">
<?php $design = '<ul class="slides"><li><div class="MainSlider">';
foreach($result as $key=>$row)
{
$design .= '<div class="MainSliderPic">'.$key.'</div>';
if((($key+1)% 15 == 0) && (count($result) != ($key+1))){
$design .= '</div></li><li><div class="MainSlider">';
}
if(count($result) == $key+1){
$design .= '</div></li></ul>';
}
}
echo $design;
?>
</div>
What I want if the result is not found from the query select, I want to show else function like:
else {
$html .= 'You have not added stock at all';
}
Because in this case I used string of html, I don't know how to echo the else statement.
More or less my codes is looking like this right now (There are many parts I have removed since it's too long)
<?php
include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if ($resultsClaim) {
while($obj = $resultsClaim->fetch_object()) {
$orders[$obj->id_cart][$obj->items] = array('status' => $obj->status ...);
}
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) { $html .= '</tr><tr>'; }
$html .= '<td>' . $item . '</td>';
$row++;
}
$html .= '<div>
<div class="member-popUpeStock'.$data['id'].' member-PopUp">
<div class="member-PopUp-box">
X
<div class="tablePopUp">
<div class="table-row">
<div class="col">: '.$data['method'].' </div>';
///HERE WHERE I WANT TO DO THAT////
else {
echo 'Nothing';
}
echo $html;
?>
Can anyone help me, please! Thanks in Advance.
Since you are taking the no of orders into a variable, you can make use of that directly to see if the orders exists or not.
$orderCount = 0; //Define orderCount in the beginning.
//Rest of the code, till foreach
foreach(){
//Rest of the code here
}
if(intval($orderCount) <= 0) //Using intval, for the case the for loop is not executed at all
{
$html = "Nothing";
}
Here you dont need to concatenate it to $html since you are displaying the table content if order exists or else you displaying "Nothing".
So $html will either have the table content or else "Nothing".
Hope this helps.
First get count of $orders
if (! empty($orders)) {
foreach ($orders AS $order_id => $order) {
// YOUR CODE HERE
}
}
else {
$html .= 'Nothing'; // Append your `no records found` message to the `$html` variable:
}
foreach ($orders AS $order_id => $order) { } else {}
So "}" is missing for foreach loop. Hope it will work for you. Let me know if you need further help.
Give it a try
<?php
include("../actions/config.php");
$resultsClaim = $mysqli->query("SELECT");
$orders = array();
$html = '';
if (!empty($resultsClaim)) {
while ($obj = $resultsClaim->fetch_object()) {
$orders [$obj->id_cart][$obj->items] = array('status' => $obj->status);
}
foreach ($orders AS $order_id => $order) {
$orderCount = count($order);
$html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
$row = 1;
foreach ($order AS $item => $data) {
if ($row > 1) {
$html .= '</tr><tr>';
}
$html .= '<td>' . $item . '</td>';
$row++;
}
$html .= '<div>
<div class="member-popUpeStock' . $data['id'] . ' member-PopUp">
<div class="member-PopUp-box">
X
<div class="tablePopUp">
<div class="table-row">
<div class="col">: ' . $data['method'] . ' </div>';
}
}
///HERE WHERE I WANT TO DO THAT////
else {
echo 'Nothing';
}
echo $html;
?>
I have this php code which takes in elements from an array and arranges the values into divs (the code starts a new row after every 3 items):
<?php
function display_products($product_id, $product){
$count = 0;
$output = "";
foreach($products as $product_id => $product) {
if (++$count % 3 == 1){
$output = $output . '<div class="offset1 span3">';
}
else {
$output = $output . '<div class="span3">';
}
$output = $output . '<div class="centre">';
$output = $output . '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
$output = $output . "</div>";
$output = $output . "</div>";
if (++$count % 3 == 0) {
$output = $output . "</div> <div class='row-fluid'>";
}
}
return $output;
}
$products = array();
$products[101] = array(
"name" => "My Product",
"img" => "img/product1.jpg"
);
?>
and have linked to this file in my index.php file like this:
<div class="row-fluid">
<?php
echo display_products($product_id, $product);
?>
</div>
my problem is, the function doesn't work. The code worked fine before I pasted it into the function.
You're defining the second variable passed into your function to be named $product, then using $products to loop through (which won't exist).
Should be:
function display_products($product_id, $products){
...
foreach($products as $product_id => $product) {