Carousel uneven when there aren't equal number of posts - php

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".

Related

How to display posts of a specific category without using ul and li tags in WordPress?

I'm trying to show 10 recent posts from a category using the code below:
<div id="slideShow" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$my_query = new WP_Query('showposts=10&cat=4');
while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="carousel-item active">
<?php if (has_post_thumbnail($post->ID)) : ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'carousel'); ?>
<img src="<?php echo $image[0]; ?>" class="slideShowImage d-block mx-auto" alt="carouselImage" width="1100" height="500">
<?php endif; ?>
<div class="carousel-caption">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
The problem is that this piece of code does not show the posts until I add ul and li tags to it:
<div id="slideShow" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<ul id="carouselPosts">
<?php
$my_query = new WP_Query('showposts=10&cat=4');
while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li class="list-unstyled">
<div class="carousel-item active">
<?php if (has_post_thumbnail($post->ID)) : ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'carousel'); ?>
<img src="<?php echo $image[0]; ?>" class="slideShowImage d-block mx-auto" alt="carouselImage" width="1100" height="500">
<?php endif; ?>
<div class="carousel-caption">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
I just want to use the code as it is without adding ul and li tags. Also, I don't want to use any plugins like Bootstrap carousel. Finally, for those who want to suggest keeping ul and li tags, I want to say as in another part of my website I've used Bootstrap card decks, I'm gonna face the same problem in
that section too. Another point is that I don't want to add extra CSS and JavaScript to my code.
After searching a lot, I found this solution and it works very well for me:
<div id="slideShow" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'published',
'cat' => '4',
'showposts' => '10',
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="carousel-item active">
<?php if (has_post_thumbnail($post->ID)) : ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'carousel'); ?>
<img src="<?php echo $image[0]; ?>" class="slideShowImage d-block mx-auto" alt="Los Angeles" width="1100" height="500">
<?php endif; ?>
<div class="carousel-caption">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>

Page navigation not working wordpress

So this is driving me kind of crazy, I just can't seem to get the page navigation to work. I installed a plugin wp-pagenavi but it still doesn't work. Is there anyone that could help me getting it to work. Here is my code (the loop):
<?php $args = array(
'post_type' => 'portfolio',
'posts_per_page'=> 10,
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'foto_video_type',
'field' => 'slug',
'terms' => 'foto'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<?php $naam = get_field('naambedrijf');
$soort = get_field('soort_uitje');
$foto = get_field('flickr_fotoset'); ?>
<div class="col s12">
<div class="title">
<h2 class="truncate" style="line-height:20px;"><?php echo $naam; ?> | <?php echo $soort; ?></h2>
<a class="fotos" href="https://www.flickr.com/photos/../sets/<?php echo $foto; ?>" target="_blank"><small>Bekijk alle foto's</small></a>
</div>
<div class="flickrphotoset">
<?php echo do_shortcode('[slickr-flickr id="" search="sets" set="' . $foto . '" size="large" items="9" bottom="10" responsive="on" type="thumbnail" galleria_options="lightbox:true;thumbnail:lazy"]'); ?>
</div>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
<?php wp_pagenavi(); ?>
And this is my archive code:
<section id="collaps">
<div class="row">
<div class="col s12 offset-s0 m12 l7 offset-l5">
<ul class="collapsible z-depth-1" data-collapsible="accordion">
<li>
<div class="collapsible-header"><i class="fa fa-camera" aria-hidden="true"></i><p>Foto</p></div>
<div id="portfolio" class="collapsible-body">
<div class="row">
<?php get_template_part('loop-foto'); ?>
<div class="col s12">
</div>
</div>
</div>
</li>
<li>
<div class="collapsible-header"><i class="fa fa-video-camera" aria-hidden="true"></i><p>Video</p></div>
<div id="portfolio" class="collapsible-body">
<div class="row">
<?php get_template_part('loop-video'); ?>
</div>
</div>
</li>
</ul>
</div>
</div>
</section>
When i add the "older" Wordpress code:
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries', '1000') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »', '1000') ?></div>
</div>
It kind of works. But page/2/ give a 404 error... What am I doing wrong?
I'm not sure if it is your case, but as I faced a similar case, I'm going to post my solution here:
In my case the problem was that I had a page with a permalink equal to the post type, so just changing the page slug solved the problem.

Wordpress Bootstrap Carousel Indicators Not Cycling

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.

Display all the posts from category

I have a category.php with script inside:
<?php $categories = get_the_category(); ?>
<?php $category_id = $categories[0]->cat_ID; ?>
<?php query_posts('cat='.$category_id); ?>
<?php while (have_posts()) : ;?>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div class="article">
<div class="article_image" style="background-image: url('<?php echo $image[0]; ?>')">
<i class="fa article_favourite"><?php wpfp_link() ?></i>
</div>
<h3><?php the_title(); ?></h3>
<div class="article_subheading_wrapper">
<p>
<!--Cut the long text -->
<?php
$string = substr(get_field("subheading"), 0, 100);
echo $string."… ";
?>
</p>
</div>
<div class="bottom">
<ul>
<li><p id="views"><img id="eye" src="<?php bloginfo('template_directory'); ?>/icons/watched_icon.svg"> <span><?php if(function_exists('the_views')) { the_views(); } ?></span></p></li>
<li><p id="likes"><i class="fa"></i><?php if(function_exists('like_counter_p')) { like_counter_p(''); }
?></p></li>
<li><p id="comments"><i class="fa fa-comment"></i> <?php comments_number( '0', '1', '%' ); ?></p></li>
</ul>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
It supposed to be displaying all the posts that belong to this category. I does, but it displays only recent one and infinite number of times.
What do I do wrong?
PLEASE PLEASE PLEASE, don't use query_posts, it is evil. Rather use WP_Query
Try either of this,
get_posts('cat=3&posts_per_page=-1');
or
query_posts( array ( 'cat' => cat_id, 'posts_per_page' => -1 ) );
Hope this helps you

Wordpress Loop in custom pages template for blog list is not working

I have created a Blog list through wordpress custom page template and assigned the same by creating a blog page.
But I am wondering the loop is correct but its not displaying any result.
http://projects.dev2d.com/msleximus/blog/
What to do. My Code ....
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<!-- #primary -->
<div role="main" class="main">
<section class="page-top">
<div class="container">
<div class="row">
<div class="span12">
<ul class="breadcrumb">
<li>Home <span class="divider">/</span></li>
<li class="active">
<?php wp_title(); ?>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="span12">
<h2> Blog </h2>
</div>
</div>
</div>
</section>
<div class="container">
<div class="row">
<div class="span9">
<?php
if ( is_page() ) {
$category = get_post_meta( $posts[0]->ID, 'category', true );
$cat = get_cat_ID( $category );
}
if ( $cat ) :
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$post_per_page = 4; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array (
'category__in' => array( $cat ),
'post_type'=> 'post',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'ignore_sticky_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign original query to temp variable for later use
global $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="blog-posts">
<article <?php post_class() ?> id="post-<?php the_ID(); ?>class="post post-medium-image">
<div class="row">
<div class="span4">
<div class="post-image">
<div class="flexslider flexslider-center-mobile flexslider-simple" data-plugin-options='{"controlNav":false, "animation":"slide", "slideshow": false, "maxVisibleItems": 1}'>
<ul class="slides">
<li> <img class="img-rounded" src="<?php the_post_thumbnail('medium'); ?>" alt="featured image"></li>
</ul>
</div>
</div>
</div>
<div class="span5">
<div class="post-content">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a></h2>
<?php the_content( 'read more »' ); ?>
</div>
</div>
</div>
<div class="row">
<div class="span9">
<div class="post-meta"> <span><i class="icon-calendar"></i>
<?php the_time( 'F jS, Y' ) ?>
</span> <span><i class="icon-user"></i> By <a href="#">
<?php the_author() ?>
</a> </span> <span><i class="icon-tag"></i>
<?php the_tags( 'Tags: ', ', ', '<br />' ); ?>
,</span> <span><i class="icon-comments"></i>
<?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?>
Read more... </div>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="pagination pagination-large pull-right">
<div class="alignleft">
<?php next_posts_link( '« Older Entries' ) ?>
</div>
<div class="alignright">
<?php previous_posts_link( 'Newer Entries »' ) ?>
</div>
</div>
</div>
</div>
<?php endif; // if ( $wp_query->have_posts() ) ?>
<?php $wp_query = $temp; //reset back to original query ?>
<div class="span3">
<aside class="sidebar">
<?php get_search_form(); ?>
<?php get_sidebar(); ?>
<div class="tabs">
<ul class="nav nav-tabs">
<li class="active"><i class="icon-star"></i> Popular</li>
<li>Recent</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="popularPosts">
<?php fanciedmedia_popular_posts(5); ?>
</div>
<div class="tab-pane" id="recentPosts">
</div>
</div>
</div>
<hr />
</aside>
</div>
<?php else : ?>
<div class="row">
<div class="span12">
<div class="post-content">
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
</div>
</div>
</div>
<?php endif; // if ( $cat ) ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Use this one and do let me know ..
<div class="container">
<div class="row">
<div class="span9">
<div class="blog-posts">
<?php query_posts('category_name = Category&showposts=10'); ?>
<?php while (have_posts()) : the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>class="post post-medium-image">
<div class="row">
<div class="span4">
<div class="post-image">
<div class="flexslider flexslider-center-mobile flexslider-simple" data-plugin-options='{"controlNav":false, "animation":"slide", "slideshow": false, "maxVisibleItems": 1}'>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
} ?>
</div>
</div>
</div>
<div class="span5">
<div class="post-content">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a></h2>
<?php the_excerpt(); ?>
Read more... </div>
</div>
</div>
<div class="row">
<div class="span9">
<div class="post-meta"> <span><i class="icon-calendar"></i>
<?php the_time( 'F jS, Y' ) ?>
</span> <span><i class="icon-user"></i> By <a href="#">
<?php the_author() ?>
</a> </span> <span><i class="icon-tag"></i>
<?php the_tags( 'Tags: ', ', ', '<br />' ); ?>
,</span> <span><i class="icon-comments"></i>
<?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?>
</div>
</div>
</div>
</article>
<?php endwhile; ?>
<?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
<div class="pagination pagination-large pull-right">
<div class="alignleft">
<div class="nav-next alignright">
<?php previous_posts_link( 'Newer posts' ); ?>
</div>
</div>
<div class="alignright">
<div class="nav-previous alignleft">
<?php next_posts_link( 'Older posts' ); ?>
</div>
</div>
</div>
<?php } ?>
</div>
</div>

Categories