I have a carousel which has to display two items per column in each item. I tried using foreach method and array_chunk() to do so, but it doesn't seem to do the work.
This is the code I am working with:
<div class="section cataloge">
<div class="section-wrapper">
<div class="container">
<div class="cataloge-page">
<?php
if ($cataloge->have_posts()) {
?>
<div class="cataloge-slider owl-carousel owl-theme">
<?php
while ($cataloge->have_posts()) {
$cataloge->the_post();
$input_array = array($cataloge);
foreach (array_chunk($input_array, 2, true) as $column) {
?>
<div class="cataloge-slider-item-wrapper">
<?php
foreach ($column as $input_array) {
?>
<article class="cataloge-item">
<figure class="cataloge-item-img img-placeholder">
<?php the_post_thumbnail(); ?>
<?php
if (!empty(get_field('sticky_text'))) {
?>
<div class="cataloge-sticky"><?php the_field('sticky_text'); ?></div>
<?php
}
?>
</figure>
<div class="cataloge-item-data">
<h4 class="cataloge-item-title"><?php the_title(); ?></h4>
<div class="cataloge-item-category">
<?php
if (have_rows('cataloge_category')) {
while (have_rows('cataloge_category')) {
the_row();
?>
<?php
if (!empty(get_sub_field('cataloge_category_item'))) {
?>
<span><?php the_sub_field('cataloge_category_item'); ?></span>
<?php
}
?>
<?php
}
}
?>
</div>
<div class="cataloge-item-info"><?php the_content(); ?></div>
<div class="cataloge-item-action">
<?php
if (!empty(get_field('cataloge_file'))) {
?>
<a href="<?php the_field('cataloge_file'); ?>">
<svg width='25' height='25'>
<use xlink:href='<?php echo get_template_directory_uri(); ?>/frontend/fontawesome/solid.svg#eye'></use>
</svg>
</a>
<?php
}
?>
<?php
if (!empty(get_field('cataloge_download_file'))) {
?>
<a href="<?php the_field('cataloge_download_file'); ?>" download="">
<svg width='25' height='25'>
<use xlink:href='<?php echo get_template_directory_uri(); ?>/frontend/fontawesome/solid.svg#download'></use>
</svg>
</a>
<?php
}
?>
</div>
</div>
</article>
<?php
}
?>
</div>
<?php
}
}
?>
</div>
<?php
}
?>
</div>
</div>
</div>
What is it that I'm missing? Is there another way, other than foreach or/and array_chunk to get the result?
There's quite a few problems that you're going to run into doing it this way. To address the main problem, you're using array_chunk on the entire query object, not just the posts. This might be why it's not working as expected. You would probably want to do array_chunk($cataloge->posts) to chunk them.
However you've put this loop in a loop of all your posts, which means each iteration of that loop will repeat this. So if you have 10 posts in your $cataloge query, you'll end up with 50 slides, with 45 of them being duplicates. If we instead use a foreach loop with the array_chunk outside of the while loop (remembering to use setup_postdata()) we should be more on the right track:
<div class="section cataloge">
<div class="section-wrapper">
<div class="container">
<div class="cataloge-page">
<?php if ($cataloge->have_posts()) { ?>
<div class="cataloge-slider owl-carousel owl-theme">
<?php $input_array = array($cataloge->posts);
foreach (array_chunk($input_array, 2, true) as $column) { ?>
<div class="cataloge-slider-item-wrapper">
<?php foreach ($column as $input_array) {
setup_postdata($column); ?>
<article class="cataloge-item">
<figure class="cataloge-item-img img-placeholder">
<?php the_post_thumbnail(); ?>
<?php if (!empty(get_field('sticky_text'))) { ?>
<div class="cataloge-sticky"><?php the_field('sticky_text'); ?></div>
<?php } ?>
</figure>
<div class="cataloge-item-data">
<h4 class="cataloge-item-title"><?php the_title(); ?></h4>
<div class="cataloge-item-category">
<?php if (have_rows('cataloge_category')) {
while (have_rows('cataloge_category')) {
the_row();
if (!empty(get_sub_field('cataloge_category_item'))) { ?>
<span><?php the_sub_field('cataloge_category_item'); ?></span>
<?php }
}
} ?>
</div>
<div class="cataloge-item-info"><?php the_content(); ?></div>
<div class="cataloge-item-action">
<?php if (!empty(get_field('cataloge_file'))) { ?>
<a href="<?php the_field('cataloge_file'); ?>">
<svg width='25' height='25'>
<use xlink:href='<?php echo get_template_directory_uri(); ?>/frontend/fontawesome/solid.svg#eye'></use>
</svg>
</a>
<?php }
if (!empty(get_field('cataloge_download_file'))) { ?>
<a href="<?php the_field('cataloge_download_file'); ?>" download="">
<svg width='25' height='25'>
<use xlink:href='<?php echo get_template_directory_uri(); ?>/frontend/fontawesome/solid.svg#download'></use>
</svg>
</a>
<?php } ?>
</div>
</div>
</article>
<?php }
wp_reset_postdata(); ?>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
You can debug by echo or print_r step by step.
I think problem here $input_array = array($cataloge);
Change to $input_array = (array) $cataloge;
Related
I have a CodeIgniter project which gets multiple data from server & display on webpage.
I wanna built something like that for all items
I have 100+ multiple items stored in database.
I wanna see first and second image when I enter website and then when I scroll page I wanna see third and fourth image and so on.
My Model:
public function getContent()
{
$query=$this->db->get('my_database');
return $query->result();
}
public function getCount(){
return $this->db->count_all('my_database');
}
My Controller:
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->library('session');
$this->load->model('My_Model');
}
public function index()
{
$contents['content']=$this->My_Model->getContent();
$contents['count']=$this->My_Model->getCount();
$this->load->view('_header');
$this->load->view('myview',$contents);
$this->load->view('_footer');
}
My View:
<?php for ( $i=0 ; $i < $count/2 ; $i++ ){ ?>
<div class="page-wrap">
<?php foreach($content as $cont){
if ($cont->item_id %2 == 1): ?>
<div class="item-left">
<a href="#">
<div class="text-part">
<h2><?php echo $cont->item_name ?></h2>
<p><?php echo $cont->item_text?></p>
</div>
<div class="image-part">
<img src="<?=base_url() ?><?php
echo $cont->item_url?>" width="500" height="275" alt="" />
</div>
</a>
</div>
<?php else: ?>
<div class="item-right">
<a href="#">
<div class="text-part">
<h2><?php echo $cont->item_name ?></h2>
<p><?php echo $cont->item_text?></p>
</div>
<div class="image-part">
<img src="<?=base_url() ?><?php
echo $cont->item_url?>" width="500" height="275" alt="" />
</div>
</a>
</div>
<?php endif; } ?>
</div> <?php } ?>
Actually these codes working perfectly when I have only 2 items.
But I have more than 100 items and my view is becoming like that
I wanna continue left, right after left, right again. But my codes are working like left, left after right, right.
I really need your help. I've tried multiple if, for loops but I couldn't make it right. There is some logic error in my view php codes but I can't find it.
I wonder what if I can get 2 items from database instead of 1 image at same time. But I couldn't get only 2 image at same time.
Just Replace this:
<?php else: ?>
<div class="item-left">
<a href="#">
<div class="text-part">
<h2><?php echo $cont->item_name ?></h2>
<p><?php echo $cont->item_text?></p>
</div>
<div class="image-part">
<img src="<?=base_url() ?><?php
echo $cont->item_url?>" width="500" height="275" alt="" />
</div>
</a>
</div>
with this:
<?php else: ?>
<div class="item-left">
<a href="#">
<div class="image-part">
<img src="<?=base_url() ?><?php
echo $cont->item_url?>" width="500" height="275" alt="" />
</div>
<div class="text-part">
<h2><?php echo $cont->item_name ?></h2>
<p><?php echo $cont->item_text?></p>
</div>
</a>
</div>
Updated Answer:
Replace your posted view code with this:
<?php $first = false; ?>
<?php foreach($content as $cont): ?>
<?php if ($cont->item_id % 2 == 1): ?>
<div class="page-wrap">
<div class="item-left">
<a href="#">
<div class="text-part">
<h2><?php echo $cont->item_name; ?></h2>
<p><?php echo $cont->item_text; ?></p>
</div>
<div class="image-part">
<img src="<?php echo base_url() . $cont->item_url;?>" width="500" height="275" alt="" />
</div>
</a>
</div>
<?php $first = true;?>
<?php else: ?>
<div class="item-right">
<a href="#">
<div class="image-part">
<img src="<?php echo base_url() . $cont->item_url;?>" width="500" height="275" alt="" />
</div>
<div class="text-part">
<h2><?php echo $cont->item_name; ?></h2>
<p><?php echo $cont->item_text; ?></p>
</div>
</a>
</div>
</div>
<?php $first = false;?>
<?php endif; ?>
<?php endforeach; ?>
<?php if($first): ?>
</div>
<?php endif;?>
Here's the div that manages the look of each item in the loop:
<div style="float:left;width:30%;padding:30px;height:auto;">
<img src="<?php echo $r['image']['sizes']['team']; ?>" style="max-width:750px !important;width:100%;">
<h3>
<?php echo $r['name']; ?>
</h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<?php echo $r['intro_text']; ?>
<?php if ($r['learn_more_text']) { ?>
Learn More
<?php } ?>
<?php if ($r['learn_more_text']) { ?>
<div id="<?php echo sanitize_title($r['name']); ?>" class="reveal-modal medium" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<h3><?php echo $r['name']; ?></h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<div class="content">
<?php echo $r['learn_more_text']; ?>
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</div>
If I set the height to a fixed number (eg: 450x), the images are aligned in a row. But that will make the page non mobile responsive. If I set the height to auto or to a percentage like 30%, the images become mobile responsive but they don't align. I get 2 images that separate into different rows.
Also, I got 2 sections of image. One is called Our Leadership and the other is called Our Team. The section at the bottom is aligned correctly and is contained in a div. The div above it refuses to be contained in a div. If I wrap it in a div, Only the first item wraps itself in the div. I've no idea why.
eg:
<center>
<div>
<div>first item here</div>
</div>
<div>second item here</div>
<div>third item here</div>
<div>fourth item here</div>
Here's the complete code for both sections:
<center><h2>Our Leadership</h2></center>
<center><div>
<?php
$people = get_field('people');
$a = 0;
foreach ($people as $r) {
$a++;
?>
<div style="float:left;width:30%;padding:30px;height:auto;">
<img src="<?php echo $r['image']['sizes']['team']; ?>" style="max-width:750px !important;width:100%;">
<h3>
<?php echo $r['name']; ?>
</h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<?php echo $r['intro_text']; ?>
<?php if ($r['learn_more_text']) { ?>
Learn More
<?php } ?>
<?php if ($r['learn_more_text']) { ?>
<div id="<?php echo sanitize_title($r['name']); ?>" class="reveal-modal medium" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<h3><?php echo $r['name']; ?></h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<div class="content">
<?php echo $r['learn_more_text']; ?>
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div></center>
<div style="clear:both;"></div>
<center><h2>Our Team</h2></center>
<center><div>
<?php
$people2 = get_field('people_bottomsection');
$a = 0;
foreach ($people2 as $r) {
$a++;
?>
<div style="float:left;width:30%;padding:30px;height:auto;">
<img src="<?php echo $r['image']['sizes']['team']; ?>" style="max-width:750px !important;width:100%;">
<h3>
<?php echo $r['name']; ?>
</h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<?php echo $r['intro_text']; ?>
<?php if ($r['learn_more_text']) { ?>
Learn More
<?php } ?>
<?php if ($r['learn_more_text']) { ?>
<div id="<?php echo sanitize_title($r['name']); ?>" class="reveal-modal medium" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
<h3><?php echo $r['name']; ?></h3>
<div class="position">
<?php echo $r['position']; ?>
</div>
<div class="content">
<?php echo $r['learn_more_text']; ?>
</div>
<a class="close-reveal-modal" aria-label="Close">×</a>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div></center>
<div style="clear:both;"></div>
Check out the source code at: http://www.equitasmg.com/who-we-are-2/
it's because the items in the loop are having different height. So, try adding a constant height to each item in the loop.
Eg:
<div style="float:left;width:30%;padding:30px;min-height:550px;"></div>
I am trying to echo a divider within a while loop after every item except the last item. Currently I have the divider after each item.
I've found a few solutions involving mysql rows and counters but so far nothing that relates to my situation.
I am using the following code (note, this does not include an attempted solution but rather my starting point.):
<?php if( have_rows('testimonials') ): ?>
<div class="testimonials col-md-4">
<h2>What People are Saying</h2>
<?php while( have_rows('testimonials') ): the_row();
// vars
$image = get_sub_field('image');
$quote = get_sub_field('quote');
$name = get_sub_field('name');
$divider = "<div class=\"dots full\"></div>";
?>
<div class="media">
<a class="pull-left" href="#">
<?php echo '<img src="'.$image['url'].'" alt="'.$image['alt'].'" class="img-circle" style="max-width: 70px;">'; ?>
</a>
<div class="media-body">
<div class="quote">
<?php echo $quote; ?>
</div>
<div class="source">
<span><?php echo $name; ?></span>
</div>
</div>
</div>
<?php echo $divider; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
I followed #Frogs advice and added the divider before each item and then used a counter to rule out the first item.
<?php if( have_rows('testimonials') ): ?>
<?php $test_count = 0; ?>
<div class="testimonials col-md-4">
<h2>What People are Saying</h2>
<?php while( have_rows('testimonials') ): the_row();
// vars
$image = get_sub_field('image');
$quote = get_sub_field('quote');
$name = get_sub_field('name');
$divider = "<div class=\"dots full\"></div>";
if ($test_count !== 0) {
echo $divider;
}
$test_count++;
?>
<div class="media">
<a class="pull-left" href="#">
<?php echo '<img src="'.$image['url'].'" alt="'.$image['alt'].'" class="img-circle" style="max-width: 70px;">'; ?>
</a>
<div class="media-body">
<div class="quote">
<?php echo $quote; ?>
</div>
<div class="source">
<span><?php echo $name; ?></span>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
This is my WordPress loop code... :)
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php if (is_category('instagram')) {?>
<?php
$__width = '225';
$__height = '225';
?>
<div id="instagram-photos">
<a href="<?php the_permalink() ?>"class="instagram-photo">
<div class="photo"><?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?> </div>
</a>
</div>
<?php }elseif(is_category('runway')) { ?>
<?php
$__width = '298';
$__height = '500';
?>
<div id="runway-category">
<a href="<?php the_permalink() ?>" class="runway-category-posts">
<div class="photo"><?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?><div class="runway-title"><?php the_title(); ?></div></div>
</a>
</div>
<?php } elseif(is_category('')){?>
<article <?php post_class(); ?>>
<div class="latest-posts">
<div class="latest-posts-info">
<div class="title"><h1><?php the_title(); ?><h1></div>
<div class="text">
<?php the_excerpt(); ?>
</div>
<div class="post-share">
<div class="facebook-like" style="float:left;">
<img src="<?php echo get_template_directory_uri(); ?>/images/facebook-icon.png" />
</div>
<div class="twitter-follow" style="float:left;">
<a href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink() ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/images/twitter-icon.png" /></a>
</div>
<div class="google-share">
<a href="https://plus.google.com/share?url=<?php the_permalink() ?>" target="_blank" ><img src="<?php echo get_template_directory_uri(); ?>/images/google-icon.png" /></a>
</div>
</div>
<div class="clear"></div><div class="post-read-more">Read More</div>
<div class="clear"></div>
</div>
<div class="latest-posts-img">
<?php //echo get_the_post_thumbnail(); ?>
<?php custom_get_post_attachments(get_the_ID(), $__width, $__height, get_the_title()); ?>
</div>
<div class="clear"></div>
</div>
</article>
<?php }?>
<?php endwhile; else: ?>
<div class="content">
<p class="not-found-p">No articles found!</p>
</div>
<?php endif; ?>
As you see I have few conditions as is_category('instagram').
The thing is: I need to limit the post quantity in exact category, not at all them.
How can I do that?
i am uncertain if we can do this in a wordpress way .. but here is the workaround --
$no_of_posts = 5;
$post_counter = 0;
if(is_Category('instagram') && ($no_of_posts<=$post_counter)) {
// bunch of code
$post_counter++;
}
untested but something like could get you going
Reversing the second div set order with separate class in foreach loop.
It will be a shelf like structure. I tried to alternate the .tl .tr class.
OR
Can we able to use css to align the second set.
Code:
<?php foreach ($products as $product) { ?>
<div class="row">
<div class="shelf">
<div class="span4"><span class="tl">
<?php if ($product['thumb']) { ?>
<img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" />
<?php } ?>
</span></div>
<div class="span2">
<div class="name"><?php echo $product['name']; ?></div>
<?php if ($product['price']) { ?>
<div class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
<?php } ?>
</div>
<?php } ?>
<div class="cart">
<input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
</div>
</div>
<div class="span2">Span 2</div>
<div class="span4"><span class="tr">Span4</span></div>
</div>
</div>
<?php } ?>
Css:
.shelf .tl {
margin-left: 58px;
}
.shelf .tr {
margin-left: -33px;
}
I think this will solve problem, it implements a counter to track whether current product should go on left or right
$product_counter = 0;
<?php foreach ($products as $product) { ?>
<?php $product_counter++; ?>
<?php if(!($product_counter%2 ==0)) { ?>
<div class="row">
<div class="shelf">
<div class="span4">
// image echo code here
</div>
<div class="span2">
// price, add to cart etc
</div>
<?php } else { ?>
<div class="span2">
// price, add to cart etc
</div>
<div class="span4">
// image echo code here
</div>
</div> <!-- shelf div closing tag -->
</div> <!-- row div closing tag -->
<?php } ?> <!-- closing else -->
<?php } ?> <!-- closing foreach-->
<!-- in case if there were odd numbers of total product, then close the row and shelf div after foreach -->
<?php if(!($product_counter%2 ==0)) { ?>
</div>
</div>
<? } ?>