WordPress: Get Post Thumbnail Outside of Loop - wordpress

I am trying to get the post thumbnail image for my carousel thumbnail indicators which are outside of the loop.
Currently it's setup with image placeholders but I need each thumb to represent the currently selected post.
<?php
$items = new WP_Query(array(
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1,
'posts_per_page' => 10,
'meta_key' => '_thumbnail_id'
));
$count = $items->found_posts;
?>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
$ctr = 0;
while ( $items->have_posts() ) :
$items->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$custom = get_post_custom($post->ID);
$link = $custom["more-link"][0];
$class = $ctr == 0 ? ' active' : '';
?>
<div class="item<?php echo $class; ?>" id="<? the_ID(); ?>">
<?php the_post_thumbnail( 'full', array (
'class' => 'img-responsive'
)); ?>
<div class="carousel-caption">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<!-- End Item -->
<?php $ctr++;
endwhile; ?>
</div>
<!-- End Carousel Inner -->
<div class="thumbs">
<div class="row">
<div class="col-md-2 active item" data-target="#myCarousel" data-slide-to="0"><img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive"></div>
<?php for($num = 1; $num < $count; $num++){ ?>
<div class="col-md-2 item" data-target="#myCarousel" data-slide-to="<?php echo $num; ?>"><img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive"></div>
<?php } ?>
</div>
</div>
</div>
<!-- End Carousel -->

Instead of:
<?php for($num = 1; $num < $count; $num++){ ?>
<div class="col-md-2 item" data-target="#myCarousel" data-slide-to="<?php echo $num; ?>">
<a href="#">
<img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive">
</a>
</div>
<?php } ?>
try using:
<?php $counter2 = 0; ?>
<?php while ( $items->have_posts() ) : $items->the_post(); ?>
<?php $counter2++; ?>
<div class="col-md-2 item <?php echo $counter2 == 1 ? 'active' : ''; ?>" data-target="#myCarousel" data-slide-to="<?php echo $counter2; ?>">
<a href="#">
<?php
the_post_thumbnail('thumbnail', array(
'class' => 'img-responsive'
));
?>
</a>
</div>
<?php endwhile; ?>
This will display it with your markup, but with a thumbnail image instead of the placeholder.

Related

Show parent child taxonomy in nested tabs with its post

I want to show parent taxonomy in tab section and the under parent taxonomy i want to display child taxonomy in nested tab. On selection of child taxonomy i want to show its post. I am getting every time child taxonomy with with its post which i don't want.
Here is what i want:
TAB1(parent active) TAB2(parent)
ALL_TAB1(child active) SUB1_TAB1 SUB2_TAB1
(posts for all, sub1_tab1, sub2_tab1 and respectively whichever is active)
What i tried
<?php
/*
Template Name: Prac
*/
get_header();
// Get list of 'categories' for tabs -->
$args = array(
'hide_empty' => false,
'parent' => 0
);
$preferences = get_terms( 'preferences', $args );
?>
<div class="row">
<div class="col-md-12">
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; ?>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<!-- Create the tabs -->
<?php
// Use counter so that 'active' class is only applied to first tab
$counter = 0;
foreach ($preferences as $preference) { ?>
<li class="nav-item">
<a class="nav-link <?= ($counter == 0) ? 'active' : '' ?>" id="<?php echo $preference->slug;?>-tab" data-toggle="tab" href="#<?php echo $preference->slug; ?>" role="tab" aria-controls="<?php echo $preference->slug;?>" aria-selected="<?= ($counter == 0) ? 'true' : 'false' ?>"><?php echo $preference->name; ?></a>
</li>
<?php $counter++; } ?>
</ul>
<div class="tab-content" id="nav-tabContent">
<!-- Get the content for each tab -->
<?php
$counter2 = 0;
foreach ($preferences as $preference) { ?>
<div role="tabpanel" class="tab-pane fade <?= ($counter2 == 0) ? 'show active' : '' ?>" id="<?php echo $preference->slug; ?>" aria-labelledby="<?php echo $preference->slug; ?>-tab">
<div class="row">
<?php
$args = array(
'post_type' => 'project',
'tax_query' => array(
array(
'taxonomy' => $preference->taxonomy,
'field' => $preference->slug,
'terms' => $preference->term_id
)
)
);
$loop = new WP_Query( $args );
?>
<div class="col-md-6" id="<?php echo $preference->slug . '-clauses' ?>">
<?php
?>
<?php
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, $preference->taxonomy, array("fields" => "all"));
?>
<?php foreach($term_list as $term_single) {
$category_children = get_terms(array(
'parent' => $term_single->term_id,
'hide_empty' => false
) );
$category_children_count = count($category_children);
?>
<?php } ?>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<?php echo $term_single->name; ?>
</li>
</ul>
<a class="col-md-6" href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?><?php echo the_title(); ?></a>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
</div> <!-- end row -->
</div> <!-- end tab-pane -->
<?php $counter2++; } ?>
</div> <!-- end tab-content -->
</div> <!-- end col -->
</div> <!-- end row -->
<!-- end content -->
<?php get_footer(); ?>
I am getting like this
TAB1 TAB2
SUB1_TAB1
post
SUB1_TAB1
post
SUB1_TAB2
Can anyone help ? Thanks.

Dynamic Bootstrap 4 Image Carousel Not Working

I have a working image bootstrap 4 carousel on my static website.
But when I want it dynamically for my WordPress theme, the images are not showing. But, in the source code, the images are showing perfectly fine.
Here is the code snippet from the image carousel:
<div id="slider" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php $args = array('cat' => 2,'post_per_page' => 4);
$slider = new WP_Query($args);
if( $slider -> have_posts() ) :
/* start the loop */
while ($slider -> have_posts() ) : $slider -> the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'full',true); ?>
<div class="carousel-item <?php if($i === 0): ?>active<?php endif; ?>">
<?php if ($thumb_url !== false) : ?>
<img class="d-block img-fluid" src="<?= $thumb_url[0] ?>" width="<?= $thumb_url[1] ?>" height="<?= $thumb_url[2] ?>" />
<?php endif; ?>
</div>
<?php endwhile; /* end of the loop */
wp_reset_postdata();
endif;
?>
</div>
<!-- Prev,Next Btn -->
<a href="#slider" class="carousel-control-prev" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a href="#slider" class="carousel-control-next" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>

ACF Pagination not working when the_content is present

I have this custom template in Wordpress which includes a script that paginates ACF fields. Here is the source code:
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="pagetitle"><?php the_title(); ?></h1>
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<?php
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$row = 0;
$files_per_page = 30; // How many images to display on each page
$files = get_field( 'fisier' );
$total = count( $files );
$pages = ceil( $total / $files_per_page );
$min = ( ( $page * $files_per_page ) - $files_per_page ) + 1;
$max = ( $min + $files_per_page ) - 1;
?>
<div class="container">
<div class="row">
<div class="col-md-8">
<section id="content">
<div class="wrapper">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php if( have_rows('fisier') ): ?>
<div class="article-files">
<h4>Files</h4>
<div class="row">
<?php while ( have_rows('fisier') ) : the_row();
$row++;
if($row < $min) { continue; }
if($row > $max) { break; }
?>
<div class="col-xs-12 file">
<?php $x = 0; ?>
<?php $file = get_sub_field('link'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php $file = get_sub_field('fisier_intern'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php if( $x == 0 ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<div class="file-pagination">
<?php
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'show_all' => false,
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => $pages
));
?>
</div> <!-- file-pagination -->
</div>
<?php endif; ?>
</div> <!-- wrapper -->
</section> <!-- content -->
</div> <!-- col -->
<div class="col-md-4">
<aside id="sidebar">
<?php get_sidebar(); ?>
</aside> <!-- aside -->
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<?php get_footer(); ?>
The code above doesn't display the pagination links but if I remove this part:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Everything works perfectly. Any idea about how I can fix this?
Thanks
Solved the problem! I had to move the pagination settings after the_content() as shown below:
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="pagetitle"><?php the_title(); ?></h1>
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<div class="container">
<div class="row">
<div class="col-md-8">
<section id="content">
<div class="wrapper">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$row = 0;
$files_per_page = 30; // How many images to display on each page
$files = get_field( 'fisier' );
$total = count( $files );
$pages = ceil( $total / $files_per_page );
$min = ( ( $page * $files_per_page ) - $files_per_page ) + 1;
$max = ( $min + $files_per_page ) - 1;
?>
<?php if( have_rows('fisier') ): ?>
<div class="article-files">
<h4>Files</h4>
<div class="row">
<?php while ( have_rows('fisier') ) : the_row();
$row++;
if($row < $min) { continue; }
if($row > $max) { break; }
?>
<div class="col-xs-12 file">
<?php $x = 0; ?>
<?php $file = get_sub_field('link'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php $file = get_sub_field('fisier_intern'); if( $file ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
<?php if( $x == 0 ): ?>
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?>
<div class="file-description"><?php the_sub_field('descriere'); ?></div>
<?php $x = 1; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<div class="file-pagination">
<?php
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'show_all' => false,
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => $pages
));
?>
</div> <!-- file-pagination -->
</div>
<?php endif; ?>
</div> <!-- wrapper -->
</section> <!-- content -->
</div> <!-- col -->
<div class="col-md-4">
<aside id="sidebar">
<?php get_sidebar(); ?>
</aside> <!-- aside -->
</div> <!-- col -->
</div> <!-- row -->
</div> <!-- container -->
<?php get_footer(); ?>

Wordpress loop only showing one record in query post

I have a Wordpress Site or its Blog section I did a WP_Query post.
The loop is within a custom template its showing only one record while I have few post entries.
I think I have done correctly...
Please do let me why it is not looping the records.
<?php get_header();
?>
<div id="body">
<div class="container">
<div class="row-fluid">
<div class="span9">
<?php
$arr = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$query = new WP_Query($arr);
if(count($query->posts) > 0):
$i = 0;
?>
<?php while($query->have_posts()): $i++;?>
<?php $query->the_post();
if(has_post_thumbnail(get_the_ID())){
$url_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
$url_thumbnail_news = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
} else {
$url_thumbnail = '<image src="'.get_template_directory_uri().'/images/no-thumbnail.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
$url_thumbnail_news = '<image src="'.get_template_directory_uri().'/images/no-thumbnail-news.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
}
?>
<?php if($i==1):?>
<div class="drop-shadow lifted"> <a class="nile-thumnail" href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>"><?php echo $url_thumbnail;?></a> </div>
<div class="the_post">
<h2 class="bj-title"><a href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>">
<?php the_title();?>
</a></h2>
<div class="bj-date">
<?php the_date();?>
<?php the_time();?>
</div>
<div class="bj-des">
<?php the_excerpt();?>
</div>
<a href="<?php the_permalink();?>" title="<?php the_title()?>" class="read-more">
<?php _e('Read more', APP_TD);?>
</a> </div>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
<?php // Reset Query
wp_reset_query(); ?>
<div class="navigation">
<div class="alignleft">
<?php previous_posts_link('« Previous') ?>
</div>
<div class="alignright">
<?php next_posts_link('More »') ?>
</div>
</div>
</div>
<div class="span3"> <?php echo get_default_sidebar();?> </div>
</div>
</div>
</div>
<?php get_footer();?>
Try removing the 'if($i==1)' and corresponding 'endif;' line

Different column width in 2 rows with bootstrap and custom post types

I'm using a custom post type on my WP website and also using bootstrap.
I want to list 5 custom post type entries on my home page, and I want the first 3 to have class col-md-4 in one row and in the next row 2 cpt entries to have the class col-md-6
At the moment I have a class col-md-4 and 2 items in the 2nd row are not centered nicely.
This is my current code:
<div class="container-fluid gray-section">
<div class="container">
<div class="row">
<?php
$projects = get_posts(array('post_type'=>'project','posts_per_page'=>5, 'order'=>'ASC'));
if ($projects) { ?>
<ul class="list-unstyled">
<?php foreach ($projects as $post) { setup_postdata( $post ) ?>
<li class="col-md-4 col-sm-6 col-xs-12 col-xxs-12 text-center ">
<a class="highlights-item" href="<?php echo get_permalink(); ?>">
<div class="highlights-container">
<?php the_post_thumbnail(); ?>
<span class="highlights-title">
<?php the_excerpt(); ?>
</span>
</div>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
<?php }
?>
</div> <!-- end row -->
</div> <!-- end container -->
</div> <!-- end container fluid -->
Any suggestions how to fix this?
This sort of issue is usually solved by adding a count to foreach loop and adding 1 each time. Then within the loop you can check what the count is and apply specific classes. e.g.
<div class="container-fluid gray-section">
<div class="container">
<div class="row">
<?php
$projects = get_posts(
array(
'post_type'=>'project',
'posts_per_page'=>5,
'order'=>'ASC'
)
);
if ($projects) { $count = 1; ?>
<ul class="list-unstyled">
<?php foreach ($projects as $post) {
setup_postdata( $post );
if( $count >= 4 ) { $extra_class = 'col-md-6'; }
else { $extra_class = 'col-md-4'; }
?>
<li class="<?php echo $extra_class; ?> col-sm-6 col-xs-12 col-xxs-12 text-center ">
<a class="highlights-item" href="<?php echo get_permalink(); ?>">
<div class="highlights-container">
<?php the_post_thumbnail(); ?>
<span class="highlights-title">
<?php the_excerpt(); ?>
</span>
</div>
</a>
</li>
<?php $count++; } wp_reset_postdata(); ?>
</ul>
<?php }
?>
</div> <!-- end row -->
</div> <!-- end container -->
</div> <!-- end container fluid -->
Give that a go...
Regards
Dan
<?php
$i = 0;
foreach ($projects as $post) { setup_postdata( $post )
?>
<li class="<?php echo ($i>2):'col-md-6'?'col-md-4' ?> col-sm-6 col-xs-12 col-xxs-12 text-center ">
<a class="highlights-item" href="<?php echo get_permalink(); ?>">
<div class="highlights-container">
<?php the_post_thumbnail(); ?>
<span class="highlights-title">
<?php the_excerpt(); ?>
</span>
</div>
</a>
</li>
<?php
$i++;
}
wp_reset_postdata();
?>

Resources