Using Packery with multiple containers? - packery

Hi i'm trying to use the packery library for laying our a look book containing various items.
Each item has an array of images which are using packery to pack them.
I've created container divs for each but packery only seems to be working on the first one. Can anyone help me?
Here is the HTML
<?php
$args = array(
'posts_per_page' => 100);
$posts = get_posts($args);
if($posts)
{
foreach($posts as $post)
{
echo '<div class="range">'; // div for each range
?>
<h1><?php the_field('range_title'); ?></h1>
<?php
$images = get_field('range_gallery');
if( $images ):
?>
<div id="slider">
<?php foreach( $images as $image ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<p><?php echo $image['caption']; ?></p>
<?php endforeach; ?>
</div>
<div id="carousel" class="packery">
<?php foreach( $images as $image ): ?>
<div class="item" >
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</div>
<?php endforeach; ?>
</div>
<?php endif;?>
<?php echo '</div>';
}
}
?>
Here is the very basic javascript:
jQuery(document).ready(function () {
var container = document.querySelector('.packery');
var pckry = new Packery( container, {
// options
itemSelector: '.item',
gutter: 10
});
});

You need to have each packery container have different class names. and then create a new instance of the Packery class for each packery container. I have taken your code and refactored it a bit so the html echos at the end of the loops.
<?php
$args = array('posts_per_page' => 100);
$posts = get_posts($args);
if($posts){
$html = '';
$script = '';
foreach($posts as $key => $post){
$html .= '<div class="range">'; // div for each range
$html .= '<h1>'.get_field('range_title').'</h1>';
$images = get_field('range_gallery');
if( $images ){
$html .= '<div id="slider">';
foreach( $images as $image ){
$html .= '<img src="'.$image['url'].'" alt="'.$image['alt'].'" />';
$html .= '<p>'.$image['caption'].'</p>';
}
$html .= '</div>';
$html .= '<div id="carousel" class="packery'.$key.'">';
foreach( $images as $image ){
$html .= '<div class="item" >';
$html .= '<img src="'.$image['sizes']['thumbnail'].'" alt="'.$image['alt'].'" />';
$html .= '</div>';
}
$html .= '</div>';
}
$html .= '</div>';
$script .= "jQuery(document).ready(function () {
var container".$key." = document.querySelector('.packery".$key."');
var pckry = new Packery( container".$key.", {
// options
itemSelector: '.item',
gutter: 10
});
});";
}
echo $html;
echo '<script>'.$script.'</script>';
}
?>

Related

Critical Error on wordpress php code when listing custom posts from a category

Recently having problems probably because of a wordpress update. This was working and now it isn't. I am using advanced custom fields to grab the category id. Any one know what is going on here?
<?php
$args = array(
'post_type' => 'messages_single',
'posts_per_page' => 10,
'cat' => '' . get_field('category') . '',
'order' => ASC,
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post(); ?>
<div class="series-item">
<div class="series-thumb">
<?php if ( has_post_thumbnail() ) { ?>
<img src="<?php the_post_thumbnail_url( 'full' ); ?>" alt="<?php the_title(); ?>" class="zoom" /></div>
<?php
}
else { ?>
<img src="/img/image-holder.png" alt="<?php the_title(); ?>" /></div>
<?php } ?>
<div class="series-info">
<h3><?php the_title(); ?></h3>
<div class="mspeaker">
<?php
if(get_field('other_speaker'))
{
echo '' . get_field('other_speaker') . '';
}
else {
echo '' . get_field('speaker') . '';
}
?>
</div>
</div><!-- .series-item-->
<?php }
wp_reset_postdata();
}
?>

Wordpress theme... error on my loop?

The loop looks good and no errors but when i paginating the loop didn't works and not change the posts showed but the page/2 or page/3 is showed in the url and by these error in the loop the posts are showed 2 hours or more late.
Elsewhere my code is:
<div class="uk-grid contenido" id="contenido">
<div class="uk-width-1-6 uk-hidden-small uk-hidden-medium"></div>
<div class="uk-width-large-2-6 uk-width-small-1-1 uk-width-medium-1-2">
<button class="uk-button-primary" type="button">Al momento</button>
<hr class="uk-grid-divider">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="uk-article">
<h2 class="uk-article-title"><?php the_title(); ?></h2>
<p class="uk-article-Meta p4d"><?php the_time('l j'); ?> de <?php the_time('F'); ?> de <?php the_time('Y'); ?> - Hace <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ); ?> | <?php $categories = get_the_category();
$separator = ','; $output = ''; if ( ! empty( $categories ) ) {foreach( $categories as $category ) {$output .= '' . esc_html( $category->name ) . '' . $separator; } echo trim( $output, $separator );} ?> | <?php if(function_exists('the_views')) { the_views(); } ?></p>
<p class="uk-article-lead"><?php echo substr(get_the_excerpt(), 0,130); ?>...<a class="more" href="<?php the_permalink(); ?>">(+)</a></p>
<div class="uk-thumbnail uk-thumbnail-large uk-overlay-hover uk-overlay-grayscale"><?php echo get_the_post_thumbnail($post->ID, 'articulo-normal-index'); ?></div>
<p class="uk-article-meta"><?php $categories = get_the_category();
$separator = ','; $output = ''; if ( ! empty( $categories ) ) {foreach( $categories as $category ) {$output .= '' . esc_html( $category->name ) . '' . $separator; } echo trim( $output, $separator );} ?> / <?php the_title(); ?></p>
<hr class="uk-article-divider">
</article>
<?php endwhile; ?>
<?php numeric_posts_nav(); ?>
<?php else : ?>
<p>Disculpa, no hay articulos</p>
<?php endif; ?>
</div>
<div class="separate uk-hidden-small uk-hidden-medium"></div>
<div class="uk-width-large-1-6 uk-hidden-small uk-width-medium-1-2 uk-block-muted min-padding">
<button class="uk-button-primary" type="button">Destacadas</button>
<hr class="uk-grid-divider">
<article class="uk-article uk-text-center">
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'destacada' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<h2 class="uk-article-title-small uk-text-center"><?php the_title(); ?></h2>
<p class="uk-article-meta uk-text-center"><?php the_time('l j'); ?> de <?php the_time('F'); ?> de <?php the_time('Y'); ?> - Hace <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ); ?></p>
<div class="uk-thumbnail uk-thumbnail-medium uk-container-center"><?php echo get_the_post_thumbnail($post->ID, 'articulo-destacadas-sidebar'); ?></div>
<hr class="uk-article-divider">
</article>
<?php
endforeach;
?>
<div class="uk-text-center data-uk-lightbox"><?php dynamic_sidebar( 'anuncio-destacadas' ); ?></div>
</div>
<?php get_sidebar(); ?>
<div class="uk-width-1-6"></div>
</div>

Wrap elements within iteration wordpress

I write the second piece of code
<?php
$subcategories = get_categories('&child_of=1&hide_empty=0'); // List subcategories of category '4' (even the ones with no posts in them)
foreach ($subcategories as $subcategory) {
$counter = 0;
$counter++;
?>
<?php if ($subcategory->parent == 1) {
if ($counter%2) {
echo '<div class="rowcat">';
}
?>
<!-- begin catblock -->
<div class="catblock">
<?php echo '<img src="'. z_taxonomy_image_url($subcategory->term_id) .'" />'; ?>
<?php echo sprintf('%s', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?>
</div>
<!-- end catblock -->
<?php
if ($counter%2) {
echo '</div>';
}
?>
<?php } ?>
<?php } ?>
For now each <div class="catblock"></div> wraps by <div class="rowcat"> but I want to make that each two blocks <div class="rowcat"> wrapped by <div class="rowcat">
So something I do wrong, what's this?
Move your $counter++ to the end of the loop:
<?php
$subcategories = get_categories('&child_of=1&hide_empty=0'); // List subcategories of category '4' (even the ones with no posts in them)
foreach ($subcategories as $subcategory) {
$counter = 0;
?>
<?php if ($subcategory->parent == 1) {
if ($counter%2) {
echo '<div class="rowcat">';
}
?>
<!-- begin catblock -->
<div class="catblock">
<?php echo '<img src="'. z_taxonomy_image_url($subcategory->term_id) .'" />'; ?>
<?php echo sprintf('%s', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?>
</div>
<!-- end catblock -->
<?php
if ($counter%2) {
echo '</div>';
}
}
$counter++;
} ?>
I found the right solution
<?php
$subcategories = get_categories('&child_of=1&hide_empty=0'); // List subcategories of category '4' (even the ones with no posts in them)
$counter = 0;
foreach ($subcategories as $subcategory) { ?>
<?php if ($subcategory->parent == 1) {
if ($counter%2 == 0) {
echo $counter > 0 ? "</div>" : "";
echo '<div class="rowcat">';
}
?>
<!-- begin catblock -->
<div class="catblock">
<?php echo '<img src="'. z_taxonomy_image_url($subcategory->term_id) .'" />'; ?>
<?php echo sprintf('%s', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?>
</div>
<!-- end catblock -->
<?php
}
$counter++;
} ?>

jQuery tools overlay only opening 1 element

So I am running in to an issue where I have a page in Wordpress querying a custom post type. It pull in the name and featured image. When you click the featured image it opens a popup with the content and some other info. Here is the link: http://pegasusemergencygrp.com.s164407.gridserver.com/who-we-are/meet-our-team/
This is the call in my page.php file:
<?php
query_posts(array('post_type' => 'doctors'));
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<aside class="doctor">
<span class="photo">
<?php the_post_thumbnail('full', array('data-rel' => "#$post->ID")); ?>
</span>
<ul>
<li><?php if(get_field('linkedin')) { echo '<span><a class="linkedin" target="_blank" href='. get_field('linkedin') .'>' . get_field('linkedin') . '</a></span>'; } ?></li>
<li><a title="<?php $values = get_field('type_of_doctor'); if($values) { foreach($values as $value) { echo '<span class='. $value .'>' . $value . '</span>'; } }?>"><?php $values = get_field('type_of_doctor'); if($values) { foreach($values as $value) { echo '<span class='. $value .'>' . $value . '</span>'; } }?></a></li>
<li><?php if(get_field('email_address')) { echo '<span class="email">' . get_field('email_address') . '</span>'; } ?></li>
<li><a title="<?php echo get_field('phone'); ?>"><?php if(get_field('phone')) { echo '<span class="phone">' . get_field('phone') . '</span>'; } ?></a></li>
</ul>
<p class="title"><?php the_title(''); ?> <?php if(get_field('location')) { echo '<span>' . get_field('location') . '</span>'; } ?></p>
<article class="simple_overlay" id="<?php the_id(); ?>">
<?php the_post_thumbnail('full'); ?>
<ul>
<li><?php if(get_field('linkedin')) { echo '<span><a class="linkedin" target="_blank" href='. get_field('linkedin') .'>' . get_field('linkedin') . '</a></span>'; } ?></li>
<li><a title="<?php $values = get_field('type_of_doctor'); if($values) { foreach($values as $value) { echo '<span class='. $value .'>' . $value . '</span>'; } }?>"><?php $values = get_field('type_of_doctor'); if($values) { foreach($values as $value) { echo '<span class='. $value .'>' . $value . '</span>'; } }?></a></li>
<li><?php if(get_field('email_address')) { echo '<span class="email">' . get_field('email_address') . '</span>'; } ?></li>
<li><a title="<?php echo get_field('phone'); ?>"><?php if(get_field('phone')) { echo '<span class="phone">' . get_field('phone') . '</span>'; } ?></a></li>
</ul>
<p class="title"><?php the_title(''); ?> <?php if(get_field('location')) { echo '<span>' . get_field('location') . '</span>'; } ?></p>
<div class="clear"></div>
<hr>
<p><?php the_content(); ?></p>
</article><!--end of .simple_overlay-->
</aside><!--end of .doctor-->
<?php endwhile; endif; wp_reset_query(); ?>
Here is my JS:
jQuery(document).ready(function() {
var imgatt = jQuery("span.photo a img").attr('data-rel');
jQuery("span.photo a img").overlay({
target: imgatt,
}); });
The script is semi working, when I click on the picture it pops open the info but if I click on the 2nd picture it pops open the first items info as well. They are both generating unique ID's on the data-rel tag with the post id and the overlay article class has the matching ID.
Source: http://jquerytools.org/demos/overlay/index.html
Solved the issue. Anyone who may stumble on this it appears jQuery Tools overlay has a hard time with the data-rel attribute on the featured image. Instead I re-configured the jQuery to find the rel tag on the anchor before the featured image and call the post id on the rel tag.
WP Code:
<a rel="#<?php the_id(); ?>" href="#<?php the_id(); ?>" class="bio"><?php the_post_thumbnail('full'); ?></a>
jQuery:
// Team Bios Overlay
jQuery(document).ready(function() {
jQuery('a.bio[rel]').overlay({
mask: '#000',
effect: 'apple'
});
});
Hope this helps someone else with a similar scenario

WordPress sticky post not showing

In my Index.php template file I have the following code which should show a featured post if it's sticky and then loop through the remaining posts below. However on the second loop shows anything and the sticky post does not appear...
The post has definitely been made sticky and second loop has successfully hidden it but the first loop just doesn't acknowledge it :/ it's not set to private and is marked as published.
Can anyone see any issues with the code? Thanks
<?php get_header(); ?>
<div class="clearfix">
<?php if(is_home()) { //if home then show the featured post and others ?>
<div class="main-column">
<?php
$sticky = get_option( 'sticky_posts' );
$sticky_query = new WP_Query( 'p=' . $sticky[0] );
// The Loop
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<div class="featured">
<article class="clearfix">
<h2><?php echo $post->post_title ?></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" />';
}?>
<p><?php the_excerpt_max_charlength(300) ?></p>
</article>
</div>
<?php endwhile; ?>
<div class="articles">
<?php $query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page' => 10 ) );
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
$count++; ?>
<div <?php if($count % 2 == 0) echo 'class="no-margin"' ?>>
<article>
<h2><?php echo $post->post_title ?></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" />';
}?>
<p><?php the_excerpt_max_charlength(150) ?></p>
</article>
</div>
<?php if($count % 2 == 0) echo '<hr/>' ?>
<?php endwhile; ?>
</div>
<?php }else if(is_search() || is_category || is_tag()){ //if other than home (search, tag, category) then show normal list (no featured style) ?>
<div class="main-column search">
<?php
$count = 0;
if(is_search()){
echo '<h1>Search Results</h1>';
} elseif(is_author()){ ?>
<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
<h1><?php echo $curauth->user_firstname; ?> <?php echo $curauth->user_lastname; ?></h1>
<div class="archive-meta">
<p><?php echo $curauth->user_description; ?></p>
</div>
<?php } else if(is_category()){ ?>
<h1><?php single_cat_title( '', true ); ?></h1>
<?php
$category_description = category_description();
if ( ! empty( $category_description ) )
echo '<div class="archive-meta">' . $category_description . '</div>';
?>
<?php }else if (is_tag()){
echo '<h1>'.the_tag().'</h1>';
}
if ( have_posts() ) : while ( have_posts() ) : the_post();
$count++; ?>
<div>
<article>
<h2><?php echo $post->post_title ?></h2>
<p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo '<a href="'.get_permalink().'">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" />';
}?>
<p><?php the_excerpt_max_charlength(300) ?></p>
</article>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php } ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
Changing the code to:
$sticky_query = new WP_Query( 'p=' . $sticky[1] );
fixes the problem but I don't know why???

Resources