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;?>
Related
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;
I've put this code to the index.php on wordpress, but it doesn't show up anything. I thought it should shows all the posts latest to newest. Anyone can solve this?
<div class="card">
<div class="card-image">
<a href="<?php echo the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID); ?>" alt="Card Image">
</a>
</div>
<div class="card-description">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<div class="card-meta">
Đăng bởi <?php the_author(); ?> vào <?php the_time('j F, Y') ?> trong <?php echo get_the_category_list(',') ?>
</div>
<p>
<?php echo wp_trim_words(get_the_excerpt(), 30); ?>
</p>
Tìm hiểu thêm
</div>
</div>
<?php }
wp_reset_query();
?>
Thank you very much.
You're missing half of a loop pretty much ...
If you want to use the post template that you have in you question, then you can do something like this:
<?php if ( have_posts() ):
$i = 0;
while ( have_posts() ):
$i++;
if ( $i > 1 ):
echo '<hr>';
endif; ?>
<div class="card">
<div class="card-image">
<a href="<?php echo the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID); ?>" alt="Card Image">
</a>
</div>
<div class="card-description">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<div class="card-meta">
Đăng bởi <?php the_author(); ?> vào <?php the_time('j F, Y') ?> trong <?php echo get_the_category_list(',') ?>
</div>
<p>
<?php echo wp_trim_words(get_the_excerpt(), 30); ?>
</p>
Tìm hiểu thêm
</div>
</div>
<?php
endwhile;
endif; ?>
Should be working.
I've made a shortcode function that returns some Advanced Custom Fields depending on which repeater field is chosen. It displays correctly and in the right order however any content typed underneath the shortcode is moved outside of its <article> container element.
Here is the code:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
</div>
</div>
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');
Can anyone help solve this mystery? Many thanks.
You have two extra closing <div>'s in your code.
Try this:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
<!--/div>
</div-->
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');
The last link here (https://soundcloud.com/waxworkfigurine2) is attached to my 'featured header text' header. Am I not closing the php tag properly?
<?php get_header(); ?>
<?php if(of_get_option('show_infoboxes') == true) { ?>
<div class="promo">
<section class=" block grid4">
<div class="col">
<?php if(of_get_option('infobox_image_1')) { ?> <a href='http://www.facebook.com/waxworkfigurine'><img src="<?php echo of_get_option('infobox_image_1') ?>"</a> <?php } ?>
<?php echo of_get_option('infobox_text_1') ?>
</div>
<div class="col">
<?php if(of_get_option('infobox_image_2')) { ?> <a href='https://twitter.com/waxworkfigurine'><img src="<?php echo of_get_option('infobox_image_2') ?>" alt=""</a> <?php } ?>
<?php echo of_get_option('infobox_text_2') ?>
</div>
<div class="col">
<?php if(of_get_option('infobox_image_3')) { ?> <a href='https://soundcloud.com/waxworkfigurine'><img src="<?php echo of_get_option('infobox_image_3') ?>" alt=""</a> <?php } ?>
<?php echo of_get_option('infobox_text_3') ?>
</div>
<div class="col">
<?php if(of_get_option('infobox_image_4')) { ?> <a href='https://soundcloud.com/waxworkfigurine2'><img src="<?php echo of_get_option('infobox_image_4') ?>" alt=""</a> <?php } ?>
<?php echo of_get_option('infobox_text_4') ?>
</div>
</section>
</div>
<?php } ?>
<section class="featured block grid2">
<h2><?php echo of_get_option('featured_header_text') ?></h2>
You need to properly close <img> tags:
<img src="<?php echo of_get_option('infobox_image_3') ?>" alt=""></a>
^-----
You are missing the closing tag for the img, add /> before </a>
<div class="col">
<?php if(of_get_option('infobox_image_4')) { ?>
<a href='https://soundcloud.com/waxworkfigurine2'>
<img src="<?php echo of_get_option('infobox_image_4') ?>" alt="" />
</a>
<?php } ?>
<?php echo of_get_option('infobox_text_4') ?>
</div>
Proper code spacing can save you lots of small erros like this, it takes a bit more space but is clearer and easier to read.
You should to terminate all of your lines with a semi-colon - e.g.
<h2><?php echo of_get_option('featured_header_text'); ?></h2>
Also I find it easier to go:
<?php if(of_get_option('infobox_image_4')) : ?> <a href='https://soundcloud.com/waxworkfigurine2'><img src="<?php echo of_get_option('infobox_image_4'); ?>" alt="" /></a> <?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