I am trying to make a Recent Posts Carousel for Bootstrap. The php seems to be outputting the proper HTML but for some reason the carousel won't cycle.
<div class="col-sm-6 col-md-9">
<div class="section-title">
<h1>Most Recent Post</h1>
</div>
<?php
// Get posts (tweak args as needed)
$i=0;
$incNum = 0;
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => "date",
'order' => "desc"
);
$posts = get_posts( $args );
?>
<div id="recent-post-carousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php foreach (array_chunk($posts, 4, true) as $posts) : ?>
<?php echo( '<li data-target="#recent-post-carousel" data-slide-to="'.$incNum.'"'.($incNum == 0 ? 'class="active"':'class').'></li>');
$incNum++;
?>
<?php endforeach; ?>
</ol>
<div class="carousel-inner">
<?php
// Get posts (tweak args as needed)
$i=0;
$incNum = 0;
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => "date",
'order' => "desc"
);
$posts = get_posts( $args );
?>
<?php foreach (array_chunk($posts, 4, true) as $posts) : ?>
<div class="item <?php if ($i==0){echo 'active';}?>">
<div class="row">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<div class="col-sm-6 post-fix">
<div class="single-post ">
<div class="pull-left post-image">
<a href="#">
<?php the_post_thumbnail( 'full' ); ?>
<i class="fa fa-angle-right"></i>
</a>
</div>
<div class="pull-right post-details">
<p class="post-date">03 Dec 2014</p>
<p><?php echo $i?></p>
<h5><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?>
</h5></a>
<span>John doe</span>
<p><?php echo substr(get_the_excerpt(), 0,99).' [...]'; ?></p>
</div>
</div>
</div>
<?php $i++ ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
Edit: I updated it now it works, but is there a more efficient way the repeating the get posts twice?
It is not required to loop posts twice, try the following method.
Methods used
wp_get_recent_posts
wp_get_attachment_url
get_post_thumbnail_id
Note: The additional details such as excerpt and date is not added to the slider, add it as per your exact requirement. Use WP Query instead of query_posts for future development.
<div class="container">
<div id="recent-post-carousel" class="carousel slide" data-ride="carousel">
<?php
$args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts($args, ARRAY_A);
// Uncomment the following lines to check output
// echo "<pre>";
// print_r($recent_posts);
$postCount = count($recent_posts);
foreach ($recent_posts as $recent):
// Change to get_the_post_thumbnail if you need to get image in certain size
$imageUri = wp_get_attachment_url(get_post_thumbnail_id($recent['ID']));
// Check if first iteration
if ($recent === reset($recent_posts)): ?>
<!-- Indicators -->
<ol class="carousel-indicators">
<?php for ($i = 0; $i <= $postCount - 1; $i++): ?>
<li data-target="#recent-post-carousel"
data-slide-to="<?php echo $i; ?>" <?php if ($i === 0) echo 'class = "active" '; ?>>
</li>
<?php endfor; ?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
endif; ?>
<div class="item <?php if ($recent === reset($recent_posts)) echo 'active'; ?>">
<img src="<?php echo $imageUri; ?>" alt="Chania" width="460" height="345">
</div>
<?php
// Check if last iteration
if ($recent === end($recent_posts)):
?>
</div>
<?php
endif;
endforeach; ?>
<!-- Left and right controls -->
<a class="left carousel-control" href="#recent-post-carousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#recent-post-carousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
I hope this helps.
Related
I'm trying to make a carousel with Bootstrap 4 posts - but something is wrong.
I only get 5 posts and I do not want to - I realize it's for 'posts_per_page' => 5 - but can anyone suggest me how to make 5 posts on one slide and 5 other posts on the other slide?
My code:
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Carousel items -->
<div class="carousel-inner">
<?php
$args = array(
'post_type' => 'auta',
'posts_per_page' =>5,
);
$my_query = new WP_Query($args);
$count = 0;
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
<div class="carousel-item <?php echo ($count == 0) ? 'active' : ''; ?>">
<div class="offset1">
<?php the_title(); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?> <?php
$limit_query = new WP_Query($args);
while ($limit_query->have_posts()) :$limit_query->the_post();$do_not_duplicate = $post->ID;?>
<div class="carousel-item">
<div class="offset1">
<?php the_title(); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<!-- Carousel nav -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
I'm new using php and I'm confused with this one; the code below is a team slider. Inside the ul it has one li with 3 divs inside (3 divs means 3 people ) Can you check my code below and enhance it. I'm confused with this one, I don't know how to do it. My code below is only adding new div(people). How can I make this possible after 3 divs it will make another li and inside it's the new batch of people,
The code below is calling the category team and it can call 9 items, I want to achieve after 3 item (Div) it will make another li so inside it the another 3 will go there.
<div class="team-wrapper">
<div id="teamSlider">
<ul class="slides">
<li>
<?php
$args = array(
'category_name' => 'team',
'post_type' => 'post',
'posts_per_page' => 9,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post(); ?>
<div class="col-md-4 wp5">
<img src="<?php the_field( 'Team_img' ); ?>" alt="Team Member">
<h2><?php the_field( 'Team_name' ); ?></h2>
<p><?php the_field( 'Description' ); ?></p>
<div class="social">
<ul class="social-buttons">
<li><i class="fa fa-dribbble"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-envelope"></i></li>
</ul>
</div>
</div>
<?php if ($x == 2) { echo ''; $x = -1; } $x++; ?>
<?php endwhile; ?>
</li>
</ul>
</div>
</div>
Note: I'm using this template: http://tympanus.net/Freebies/HalcyonDaysTemplate/ You can download it here: http://tympanus.net/codrops/2014/07/14/freebie-halcyon-days-one-page-website-template/ you can check the WE'RE A TEAM THAT ADORE WHAT WE DO section
Not tested, but try this:
<div class="team-wrapper">
<div id="teamSlider">
<ul class="slides">
<?php
query_posts(array(
'category_name' => 'team',
'post_type' => 'post',
'posts_per_page' => 9,
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1)
));
$x = 1;
while (have_posts()) :
the_post();
if ($x == 1) :
?>
<li>
<?php endif; ?>
<div class="col-md-4 wp5">
<img src="<?php the_field( 'Team_img' ); ?>" alt="Team Member">
<h2><?php the_field( 'Team_name' ); ?></h2>
<p><?php the_field( 'Description' ); ?></p>
<div class="social">
<ul class="social-buttons">
<li><i class="fa fa-dribbble"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-envelope"></i></li>
</ul>
</div>
</div>
<?php
if ($x == 3) :
$x = 1;
?>
</li>
<?php
endif;
$x++;
?>
</ul>
</div>
</div>
As per the html i can see in the link you shared, there needs to be a li created for each item within ul. The 3 items logic will be taken care of by the javascript driving that plugin.
For getting one li per item, you need to loop it outside li rather than inside li as is the case currently.
Updated Code:
<div class="team-wrapper">
<div id="teamSlider">
<ul class="slides">
<?php
$args = array(
'category_name' => 'team',
'post_type' => 'post',
'posts_per_page' => 9,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
while(have_posts()){ ?>
<li>
<div class="col-md-4 wp5">
<img src="<?php the_field( 'Team_img' ); ?>" alt="Team Member">
<h2><?php the_field( 'Team_name' ); ?></h2>
<p><?php the_field( 'Description' ); ?></p>
<div class="social">
<ul class="social-buttons">
<li><i class="fa fa-dribbble"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-envelope"></i></li>
</ul>
</div>
</div>
</li>
<?php } ?>
</ul>
</div>
</div>
I am trying to build an image slider / carousel using Bootstrap. The images should be shown from the Wordpress post categories 'Aktuell' and 'Referenz' which I have created. The code below works perfectly for me when I want to show 1 image from the category 'Aktuell' and 3 images from the category 'Referenz'.
<div id="myCarousel" class="carousel slide hidden-sm hidden-xs ">
<div class="carousel-inner">
<?php
$the_query = new WP_Query(array(
'category_name' => 'Aktuell',
'posts_per_page' => 1,
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item active">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('full'); ?>
<div class="carousel-caption">
<h3><?php the_title();?></h3>
<p><?php the_excerpt();?></p>
<h1>»</h1>
</div>
</a>
</div><!-- item active -->
<?php
endwhile;
wp_reset_postdata();
?>
<?php
$the_query = new WP_Query(array(
'category_name' => 'Referenz',
'posts_per_page' => 3,
'orderby' => 'rand'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('slider');?>
<div class="carousel-caption">
<h3><?php the_title();?></h3>
<p><?php the_excerpt();?></p>
<h1>»</h1>
</div>
</a>
</div><!-- item -->
<?php
endwhile;
wp_reset_postdata();
?>
</div><!-- carousel-inner -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div><!-- #myCarousel -->
What I want to do is to show 3 images from each category. So when I use 'posts_per_page' => 3, on line #6, the slide function will not work anymore when I click on the left or right arrows to slide. Instead the images are getting displayed below each other.
how can I get this fixed?
I could find a way to make it work like this:
function the_carousel_item($post, $post_per_page, $offset = 0, $orderby = 'rand', $active = NULL) {
$the_query = new WP_Query(array(
'category_name' => $post,
'posts_per_page' => $post_per_page,
'orderby' => $orderby,
'offset' => $offset
));
$active_css_class = (isset($active)) ? $active : '';
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="item <?= $active_css_class ?>">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('slider');?>
<div class="carousel-caption">
<h3><?php the_title();?></h3>
<p><?php the_excerpt();?></p>
<h1>»</h1>
</div>
</a>
</div><!-- item -->
<?php
endwhile;
wp_reset_postdata();
}
<div id="myCarousel" class="carousel slide hidden-sm hidden-xs ">
<div class="carousel-inner">
<?php
// Display the starter image from the post 'Aktuell'
the_carousel_item('Aktuell', 1, 0, 'ID', 'active');
// Display posts from 'Aktuell'
the_carousel_item('Aktuell', 3, 1, 'ID');
// Display posts from 'Referenz'
the_carousel_item('Referenz', 3);
?>
</div><!-- carousel-inner -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div><!-- #myCarousel -->
<div class="container inner-cont container-contact-field">
<div class="row mobile contact-field">
<div class="col-xs-8 col-xs-offset-2">
<?php dynamic_sidebar('footer_1'); ?>
<?php dynamic_sidebar('footer_2'); ?>
</div>
</div>
</div>
You want showing 1 image above (Aktuell) and 3 image slider below (Referenz), or 1 above 1 slider, or what?
If 3 image below, your slider will not work because you already set 'posts_per_page' => 3
If I don't have 8 posts the carousel looks like this.
Here is the Wordpress loop
<div class="section-title">
<h1>Most Recent Post</h1>
</div>
<div id="recent-post-carousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#recent-post-carousel" data-slide-to="0" class="active"></li>
<li data-target="#recent-post-carousel" data-slide-to="1"></li>
</ol>
<div class="carousel-inner">
<?php
// Get posts (tweak args as needed)
$i=0;
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => "date",
'order' => "desc"
);
$posts = get_posts( $args );
?>
<?php foreach (array_chunk($posts, 4, true) as $posts) : ?>
<div class="item <?php if ($i==0){echo 'active';}?>">
<div class="row">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<div class="col-sm-6">
<div class="single-post">
<div class="pull-left post-image">
<a href="#">
<?php the_post_thumbnail( 'full' ); ?>
<i class="fa fa-angle-right"></i>
</a>
</div>
<div class="pull-right post-details">
<p class="post-date">03 Dec 2014</p>
<p><?php echo $i?></p>
<h5><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?>
</h5></a>
<span>John doe</span>
<p><?php echo substr(get_the_excerpt(), 0,99).' [...]'; ?></p>
</div>
</div>
</div>
<?php $i++ ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
Not sure if it has something to do with wordpress, i tried it on a static page and it worked fine. one way to fix it is to remove left or right margin from the col-sm-6, but i don't think this is a good way to do things.
I have created a fiddle for you. In fiddle above one is your case and the below one is Case with min-height(solved).
**https://jsfiddle.net/Anuj_Kumar/L5uduxLr/1/embedded/result/**
If still it's not fixed then try adding class "cleafix" with "single-post".
I tried few plugins for having featured product slider carousal for my woocommerce wordpress site but they didn't work as i intended so i tried to create on my own. Its almost worked except that it is displaying additional posts that don't exist at all. They don't exist even as products. First picture shows the product that is featured. Second picture is extra content that don't exist at all. The number of such unnecessary posts is equal to the multiple of total number of featured products. Suggestion on similar question reflects almost what i have done but It have problems. In my case i have 5 featured products and it is displaying 25 unnecessary posts. Currently, i tried just to display one item at a time and after fixing this issue i will display 3 posts at a time so it loops twice giving 6 posts to slide.
<div id="featured" class="carousel slide ">
<div class="carousel-inner ">
<?php
$args = array( 'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 8,
'post_status' => 'publish',
'offset' => 0,
'numberposts' => 6,
//'orderby' =>'rand',
'order' => 'DESC'
);
$featured_loop = new WP_Query( $args );
//echo "<pre>";
//print_r($featured_loop);
//echo "</pre>";
if ( $featured_loop->have_posts()){
$i = 1; $count;
for ($count=0; $count < 6;) {
foreach ( $featured_loop as $featured ) {
$featured_loop->the_post();
?>
<div class=
<?php
echo '"';
echo 'item ';
if ($i == 1) {
echo 'active';
}
echo '"';
?>>
<div class="col-xs-6 col-sm-4 col-md-4 col-lg-4 ">
<div class="thumbnail">
<i class="tag"></i>
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php
if (has_post_thumbnail( $featured->post->ID )){
echo get_the_post_thumbnail($featured->post->ID, 'shop_catalog');
}
else {
echo '<img width ="150" src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" class="img-responsive img-rounded" />';
}
?>
</a>
</div><!-- thumbnail -->
<div class="panel-body text-center">
<h6><?php the_title(); ?> </h6>
</div><!-- panel-body text-center -->
</div><!-- col-xs-6 col-sm-4 col-md-4 col-lg-4 -->
</div>
<?php
$i++;
}
$count++;
}
}
?>
</div><!-- carousal item class ends -->
</div><!-- carousal inner ends -->
<a class="left carousel-control" href="#featured" data-slide="prev"><i class="fa fa-arrow-left"></i></a>
<a class="right carousel-control" href="#featured" data-slide="next"><i class="fa fa-arrow-right"></i></a>
<?php wp_reset_postdata(); wp_reset_query(); ?>
</div><!-- carousel slide -->
First of all, your iteration is wrong. You are first running a malformed for loop n= 6 times, then for each integer from 0 to 5 you a running a foreach loop m times, resulting in a malformed loop with complexity of O(n*m) that don't solve your problem.
Here's your code, rewritten. Hope it helps.
<div id="featured" class="carousel slide ">
<div class="carousel-inner ">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 6,
'post_status' => 'publish',
'offset' => 0,
'order' => 'DESC'
);
$featured_loop = new WP_Query( $args );
if ( $featured_loop->have_posts() ):
while ( $featured_loop->have_posts() ) : $featured_loop->the_post(); ?>
<div class="<?php echo 'item'; ?>">
<div class="col-xs-6 col-sm-4 col-md-4 col-lg-4 ">
<div class="thumbnail">
<i class="tag"></i>
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo woocommerce_get_product_thumbnail(); ?>
</a>
</div>
<div class="panel-body text-center">
<h6><?php the_title(); ?> </h6>
</div>
</div>
</div>
<?php endwhile; ?>
<a class="left carousel-control" href="#featured" data-slide="prev"><i class="fa fa-arrow-left"></i></a>
<a class="right carousel-control" href="#featured" data-slide="next"><i class="fa fa-arrow-right"></i></a>
<?php wp_reset_postdata(); endif; ?>
</div>
</div>
<?php wp_reset_query(); ?>
Answering this with help from #Sorin Gheata. He forgot to make it work as bootstrap carousel.
<div id="featured" class="carousel slide ">
<div class="carousel-inner ">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'numberposts' => 6,
'posts_per_page' => 6
);
$featured_loop = new WP_Query( $args );//global $product;
if ( $featured_loop->have_posts() ):
while ( $featured_loop->have_posts() ) : $featured_loop->the_post(); ?>
<div class=
<?php
echo '"';
echo 'item ';
if ($i == 1) {
echo 'active';
}
echo '"';
?>>
<div class="col-xs-6 col-sm-4 col-md-4 col-lg-4 ">
<div class="thumbnail">
<i class="tag"></i>
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo woocommerce_get_product_thumbnail(); ?>
</a>
</div>
<div class="panel-body text-center">
<h6><?php the_title(); ?> </h6>
</div>
</div>
</div>
<?php $i++; endwhile; ?>
<a class="left carousel-control" href="#featured" data-slide="prev"><i class="fa fa-arrow-left"></i></a>
<a class="right carousel-control" href="#featured" data-slide="next"><i class="fa fa-arrow-right"></i></a>
<?php wp_reset_postdata(); endif; ?>
</div>
</div>
<?php wp_reset_query(); ?>