How to output thumbnail twice in a WordPress loop - php

ok I have a portfolio page. Every portfolio item has thumbnail attached. what I want is to print out the first thumbnail from the first post twice. One at the beginning & other at the ending. like: image_1, image_2, image_3, image_4 & then image_1 again.
my code is:
<div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;">
<?php $args = array( 'post_type' => 'portfolio', 'order' => 'dsc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $extraFirstClass = $loop->current_post == '0' ? ' main-image-porfolio-main' : ''; ?>
<?php
$attributes = array(
"class" => "main-image portfolio " . $extraFirstClass,
"id" => "photo_{$post->ID}",
);
the_post_thumbnail("portfolio_thumb", $attributes);
?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<div class="portfolio-box">
<h5>Portfolio</h5>
<ul class="item-list" id="portfolio-list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
How can I do it?

As previously answered by me here, I would use PHP for. However, if for some reason you don't want that, I don't think you would need to resort to two loops for this. You can do the following:
<?php
$args = array( 'post_type' => 'portfolio', 'order' => 'dsc');
$loop = new WP_Query( $args );
$first = $loop->posts[0];
$attributes = array(
"class" => "main-image portfolio",
"id" => "photo_{$loop->posts[$i]->ID}",
);
<div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;">
for( $i = 0; $i < count($loop->posts); $i++ ) {
$attrs = $attributes;
$attrs["class"] .= $i === 0 ? ' main-image-portfolio-main' : '';
echo get_the_post_thumbnail( $loop->posts[$i]->ID, "portfolio_thumb", $attrs );
}
echo get_the_post_thumbnail( $first->ID, "portfolio_thumb", $attrs );
?>
<div class="portfolio-box">
<h5>Portfolio</h5>
<ul class="item-list" id="portfolio-list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
<li>
<?php echo $first->post_title;?>
</li>
</ul>
</div>
</div>

here is the code, edit it according to your needs:
$(document).ready(function()
{
var imagesrc = $(".example post's image class").attr("src");
$(".the container or post after which you want to put your image").appendTo("<img src='"+imgsrc+"' alt='image' />");
});

Related

the_field - ACF not working in 'while' loop

Main string of code (that doesn't work):
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3,
'type_of_site' => 'landing_page_type',
);
$webTariffs = new WP_Query($args);
while ($webTariffs->have_posts()) {
$webTariffs->the_post();?>
<div class="pricing-item">
<div class="pricing-item-header">
<h3><?php the_title() ?></h3>
<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
</div>
</div>
<?php } ?>
The ways i tried to solve this problem:
put insted of the_field('tariff_price_kn') - echo('hi') - the code worked
add $post_id -> the_field('tariff_price_kn', $post_id)
$currencyKune = get_field('tariff_price_kn'); And then echo $currencyKune
P.S 3) i don't know exactly where should i put $currencyKune = get_field('tariff_price_kn'), so i put it before while at first time - doesn't work and at the second time i put it after $webTariffs->the_post();
Try this :
<?php
$args = array(
'post_type' => 'tariffs',
'posts_per_page' => 3
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-6">
<h2 class="the-title"><?php the_field('tariff_price_kn', $post->ID); ?> + <?php the_title() ;?> </h2>
</div>
<?php endwhile; else: ?> Nothing here <?php endif; ?>
<?php wp_reset_query(); ?>

Editing PHP grid page for WordPress site

I would like to Edit the Grid Artist page to only have 12 artists and land on the producers section instead of all artist.
Here' s the link of website page : http://cascaderecords.fr/roster/
PHP code:
<?php
if (ci_setting('artists_isotope') == 'enabled' AND !wp_is_mobile() AND !is_tax() ):
?>
<ul class="filters-nav group">
<li><?php _e('All Artists', 'ci_theme'); ?></li>
<?php
$args = array('hide_empty' => 1);
$cats = get_terms('artist-category', $args);
?>
<?php foreach ( $cats as $cat ): ?>
<li><?php echo $cat->name; ?></li>
<?php endforeach; ?>
</ul>
<?php
endif; // is isotope enabled
global $paged;
if ( ci_setting('artists_isotope') == 'enabled' AND !wp_is_mobile() AND !is_tax() ) {
$args = array(
'post_type' => 'cpt_artists',
'posts_per_page' => -1
);
} else {
$args = array(
'post_type' => 'cpt_artists',
'posts_per_page' => ci_setting('artists_per_page'),
'paged' => $paged
);
}
$the_query = !empty($the_query) ? $the_query : new WP_Query($args);
?>
<article class="row">
<ul class="col-md-12 items-list-grid filter-container <?php echo $filter; ?>">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $cats = wp_get_object_terms($post->ID, 'artist-category'); ?>
<li class="<?php foreach ( $cats as $cat ) : echo $cat->slug.' '; endforeach; echo $grid; ?>">
<article id="artist-<?php the_ID(); ?>" <?php post_class('row'); ?>>
<div class="col-md-12">
<?php if ( has_post_thumbnail() ) : ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('ci_thumb_square'); ?>
</a>
</figure>
<?php endif; ?>
<h2><?php the_title(); ?></h2>
</div><!-- /col-md-12 -->
</article><!-- /row -->
</li><!-- /col-md-4 -->
<?php endwhile; wp_reset_postdata(); ?>
</ul>
Try change:
'posts_per_page' => -1
to:
'posts_per_page' => 12
Not sure what "ci_setting('artists_per_page')" is in below variable, need more info:
'posts_per_page' => ci_setting('artists_per_page'),

php pagination full list of pages instead of small list with next and prev

My pagination code shows a full list of pages like 1,2,3,4,5,6,7,8,9,10 but i want it to show Prev,1,2,3,4,5,Next instead how can i do this with this code
if($tot>$perq) {
$output.="<div class='pagenation'>";
for($i=0;$i<($tot/$perq);$i++) {
$j=$i+1;
if($pg==$i)
$output.="<a href='".get_permalink()."?pg=$i' class='button active'>".$j."</a>";
else
$output.="<a href='".get_permalink()."?pg=$i' class='button'>".$j."</a>";
}
$output.="</div>";
}
Here is an example from a site I am working on. The keywords are 'previous_posts_link' and 'next_posts_link', which together with the $arg 'posts_per_page' will paginate for you.
Hope it helps.
<?php
the_content();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'bonsai','posts_per_page' =>12,'paged' => $paged);
$myposts = query_posts($args);
?>
<span id="tree-list-span">
<fieldset>
<legend>The Bonsai</legend>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="main_bonsai_list_item">
<a href="<?php echo get_permalink($post->id);?>">
<span class="grower_name"><?php the_title();?></span><br/>
<span class="grower_name">
<?php
$attr = array(
'id' => "link-".$post->ID,
'style' => 'max-width: 120px',
);
echo get_the_post_thumbnail($id,'bonsai-list-thumb',$attr);
?>
</span><br/>
<?php $custom = get_post_custom($post->ID);?>
<?php $grower = $custom["tree_grower"][0];?>
<span class="grower_name"><?php echo $grower;?></span>
</a>
</li>
<?php endforeach;
?>
</ul>
<span id="page_links">
<?php
previous_posts_link( '«' );
next_posts_link( '»', $myposts->max_num_pages );
wp_reset_postdata();
?>
</span>
</fieldset>
<?php
?>

WordPress add Pagination to custom post type

In WordPress i'm currently using the below code to display both the content of the testimonials page and the posts from the custom post type testimonials.
I'm trying to add pagination but to no avail, no errors appear they just don't display. I'm guessing this probably has something to do with the fact i'm using two loops and having to reset, but i'm not sure?
Any more infomation required please ask.
<?php /* Template Name: Testimonials */ get_header(); ?>
<div id="main">
<div class="fullwidth">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<h1><?php the_field('h1_title'); ?></h1>
<div class="width">
<ul class="menu slider">
<?php if(wp_list_pages('child_of='.$post->ID.'&echo=0&exclude=2')) { $list_of_pages = wp_list_pages('echo=0&title_li=&depth=1&child_of='.$post->ID.'&exclude=2'); $list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages); for ($i = 0; $i <= $len-2; $i++) : echo $list_pages[$i] . '<li class="list-item-' . ($i+1) . ' '; endfor; echo $list_pages[$i]; }
elseif(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) { $list_of_pages = wp_list_pages('echo=0&title_li=&depth=1&child_of='.$post->post_parent.'&title_li=&exclude=2'); $list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages); for ($i = 0; $i <= $len-2; $i++) : echo $list_pages[$i] . '<li class="list-item-' . ($i+1) . ' '; endfor; echo $list_pages[$i]; } ?>
</ul>
</div>
<h2><?php the_field('h2_title'); ?></h2>
<?php the_content(); ?>
<?php endwhile; wp_reset_postdata(); ?>
<div class="posts">
<?php $query = new WP_Query( array ( 'post_type' => 'testimonial', 'order' => 'DESC', 'posts_per_page' => 9, 'orderby'=> menu_order ) ); while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="blog-post">
<div class="content">
<h3><?php the_title();?></h3>
<h4><?php the_field('visit_date'); ?></h4>
<?php the_content(); ?>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
</div>
</div>
<?php get_footer(); ?>
I feel you are not getting and passing the page#, try to change/replace your code like this
$query = new WP_Query( array ( 'post_type' => 'testimonial', 'order' => 'DESC', 'posts_per_page' => 9, 'orderby'=> menu_order ) ); while ( $query->have_posts() ) : $query->the_post();
to
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array ( 'post_type' => 'testimonial', 'order' => 'DESC', 'posts_per_page' => 9, 'orderby'=> 'menu_order','paged' => $page ) );
while ( $query->have_posts() ) : $query->the_post();

Image Carousel data-slide-to="?" with WordPress PHP

I have a custom post type in WordPress to add a list of featured images to.
These featured images are pulled into a Bootstrap carousel via WP_Query.
Everything is working great: images are being pulled fine and I've managed to get the carousel indicators to show up if a thumbnail exists. I currently have 3 in the custom post type.
My question is this: normally (statically) one would create the carousel indicators with a list and there is a data-slide-to="0", "1", "2", etc. to allow you to click the indicator to view the slide for each one.
When working with WordPress and PHP, how can I set it up so it knows to count up from Zero automatically, based on the number of slides?
In the following code, it's setup up manually and will work fine for 1 and 2 but any slides added in addition will take on the number 1 as its data-slide-to number.
<div class="featured-carousel">
<div class="carousel-shadow">
<div id="carousel-home-featured" class="carousel-fade slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php
$items = new WP_Query(array(
'post_type' => 'home-slider',
'posts_per_page' => 1
));
while ( $items->have_posts() ) :
$items->the_post();
?>
<?php
if ( '' != get_the_post_thumbnail() ) { ?>
<li data-target="#carousel-home-featured" data-slide-to="0" class="active"></li>
<?php } else { ?>
<?php } ?>
<?php endwhile; ?>
<?php
$items = new WP_Query(array(
'post_type' => 'home-slider',
'posts_per_page' => 10,
'offset' => -1,
));
while ( $items->have_posts() ) :
$items->the_post();
?>
<?php
if ( '' != get_the_post_thumbnail() ) { ?>
<li data-target="#carousel-home-featured" data-slide-to="1"></li>
<?php } else { ?>
<?php }
?>
<?php endwhile; ?>
</ol>
<div class="carousel-inner">
<?php
$items = new WP_Query(array(
'post_type' => 'home-slider',
'posts_per_page' => 1
));
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];
?>
<div class="item active" id="<? the_ID(); ?>"> <?php echo get_the_post_thumbnail($post->ID, 'full', array('class'=>"img-responsive"));?> </div>
<?php endwhile; ?>
<?php
$items = new WP_Query(array(
'post_type' => 'home-slider',
'posts_per_page' => 10,
'offset' => -1,
));
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];
?>
<div class="item" id="<? the_ID(); ?>"> <?php echo get_the_post_thumbnail($post->ID, 'full', array('class'=>"img-responsive"));?> </div>
<?php endwhile; ?>
</div>
</div>
</div>
</div>
You can do it using just one WP_Query call and actually fetch only the items with featured images by passing the 'meta_key' => '_thumbnail_id' parameter to WP query. I'd revise your code to:
<?php
$items = new WP_Query(array(
'post_type' => 'home-slider',
'posts_per_page' => 10,
'meta_key' => '_thumbnail_id'
));
$count = $items->found_posts;
?>
<div class="featured-carousel">
<div class="carousel-shadow">
<div id="carousel-home-featured" class="carousel-fade slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-home-featured" data-slide-to="0" class="active"></li>
<?php for($num = 1; $num < $count; $num++){ ?>
<li data-target="#carousel-home-featured" data-slide-to="<?php echo $num; ?>"></li>
<?php } ?>
</ol>
<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 echo get_the_post_thumbnail($post->ID, 'full', array('class'=>"img-responsive"));?> </div>
<?php $ctr++;
endwhile; ?>
</div>
</div>
</div>
</div>

Categories