Isotope JS Library - Cards overlapping - javascript

I have integrated the Isotope grid .js library (https://isotope.metafizzy.co/index.html) with my Wordpress posts to display a nice grid: see screenshot below:
However, on initial load (and when I do a cache reload) the grid items seem to overlap. See below Screenshot:
I've enqueued the js library using a CDN, I can tell thats working. I have the below code. Firstly the loop to bring in the posts and then the bit of Javascript that initialises it.
<div class="container blog-card-container">
<div class="grid">
<?php
$my_query = new WP_Query('cat=[14]&posts_per_page=10');
?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;?>
<div class="grid-sizer"></div>
<!-- Conditional a Link =========================================== -->
<?php
if(get_field('quote') == ''){
$yourTag = "<a href='".get_the_permalink()."'>" ;
} else {
$yourTag = "";
}
?>
<div> <?php echo $yourTag; ?> </div>
<div class="grid-item">
<div class="client-header-logo-card" style="background-color: <?php the_field('client_brand_colour'); ?>;">
<?php
$image = get_field('client_logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<div class="clients-card-block">
<h2><?php the_title(); ?></h2>
<?php if( get_field('quote') ): ?><p class="client-quote"><span style="color:<?php the_field('client_brand_colour'); ?>; font-weight:bold;">“ </span><?php the_field('quote'); ?><span style="color:<?php the_field('client_brand_colour'); ?>;font-weight:bold;"> ”</span></p><?php endif; ?>
<?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
<?php if( get_field('quote_position') ): ?><p class="client-position" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_position'); ?></p><?php endif; ?>
<?php if( get_field('button_text') ): ?>
<a class="btn btn-sm btn-client-archive" href="<?php the_permalink(); ?>" style="background-color:<?php the_field('client_brand_colour'); ?>;" role="button"><?php the_field('button_text'); ?></a>
<?php endif; ?>
<?php if( get_field('video_url') ): ?>
<div class="embed-container">
<?php the_field('video_url'); ?>
</div>
<?php endif; ?>
</div>
</div>
</a>
<?php endwhile; ?>
</div>
</div>
<script type="text/javascript">
// vanilla JS
var grid = document.querySelector('.grid');
var iso = new Isotope( grid, {
itemSelector: '.grid-item',
percentPosition: true,
masonry: {
columnWidth: '.grid-sizer',
gutter: 20
}
});
// layout Isotope after each image loads
$grid.imagesLoaded().progress( function() {
$grid.isotope('layout');
});
</script>
I included this little bit of javascript after the init which is supposed to fx the issue, but I find them still overlapping:
// layout Isotope after each image loads
$grid.imagesLoaded().progress( function() {
$grid.isotope('layout');
});
My site is here by the way: http://webserver-meetandengage-com.m11e.net its around halfway down the homepage.
Do you think this is because I've included the javascript on the same page? Should I include it in it's own .js file? I tried this initially but I couldn't get it to work properly...

Seems you didn't include imagesLoaded plugin on the page. Please make sure that the plugin is added properly. Hope that'll resolve the overlapping issue. And you should add this <div class="grid-sizer"></div> outside the loop.

Related

How to fix image not displaying in php?

So I am trying to create a CTA(call to action) adding three cards into my wordpress site. I am trying to insert PHP and ACF functions so that it shows my image, currently it displays the title and the body text of these cards but doesn't show any image? How do I fix it?
In my acf, I have created four sub field types: "cta_image", "cta_title", "intro_text" and "cta_link". In my code editor, I have inserted the following functions and so far on my wordpress site, it shows the title and the intro text but I don't know how to put an image.
<?php
if ( have_rows( 'ctas' ) ) :
// Loop through rows (parent repeater).
while ( have_rows( 'ctas' ) ) :
the_row();
?>
<div class="title-wrapper">
<?php if ( get_sub_field( 'cta_title' ) ) : ?>
<h2><?php the_sub_field( 'cta_title' ); ?></h2>
<?php endif; ?>
</div>
<div class="body-text">
<?php print_r(get_sub_field('intro_text')); ?>
</div>
<?php
endwhile;
endif;
?>
Every time I insert functions to show the image, it gives a database error or simply doesn't show any image. The final work is supposed to look like the image down below. Mine shows just the title and body text all in a single line.
Have you tried using this bit of code directly from the documentation?
<?php $image = get_sub_field('cta_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>

jQuery - Get Dynamic (*) Element By Id and put on array

I'm working to create a classifieds site. On the search page will be presented multiple products cards. Each product card can have more than one image. When that happens, I will uses a slider.
To build this slider, I'm using a jquery library (bxslider). The application is based on WordPress.
So far, everything was going well. Cards and slider working. But when I have more than one card (post), the slider controls change the position on all cards. Of course, since the selector is the same for everyone.
Then I added the post ID to the ID selector, making every single card. But now must do jQuery understand that there are several unique cards for him to apply the function that creates the slides within each card.
My PHP Code
<?php
$id = get_the_ID();
?>
<div id="card-slider-<?php echo $id?>">
<?php
$images = get_post_meta($post->ID, 'vdw_gallery_id', true);
foreach ($images as $image) { ?>
<li>
<div class="card-slide-item">
<img src="<?php echo wp_get_attachment_url( $image, 'imob-thumbs' ); ?>">
</div>
</li>
<?php }
?>
</div>
<div class="card-slide-prev"></div>
<div class="card-slide-next"></div>
My jQuery
(function( $ ){
$.fn.sliderID = function() {
var sliders = [];
sliders = $('[idˆ=card-slider-]').length;
console.log('sliders ids', sliders);
};
})( jQuery );
$(document).ready(function(){
$.fn.sliderID();
});
Instead of giving each slider an individual id card-slider-{id}, consider to give them all a common class card-slider and initiate the slider plugin on all elements that have that class. You can pass along the ids of the prev/next selectors (which you can e.g. store in a data attribute) in the initialization arguments of the slider:
Slider initiation
$( ".card-slider" ).each(function( index ) {
var slider_id = $(this).data('id');
$( this ).bxSlider({
nextSelector: '#card-slide-'+slider_id+'-next',
prevSelector: '#card-slide-'+slider_id+'-prev',
});
});
Markup:
<?php
$id = get_the_ID();
?>
<div class="card-slider" data-id="<?php echo $id; ?>">
<?php
$images = get_post_meta($post->ID, 'vdw_gallery_id', true);
foreach ($images as $image) { ?>
<li>
<div class="card-slide-item">
<img src="<?php echo wp_get_attachment_url( $image, 'imob-thumbs' ); ?>">
</div>
</li>
<?php }
?>
</div>
<div id="card-slide-<?php echo $id; ?>-prev"></div>
<div id="card-slide-<?php echo $id; ?>-next"></div>

Wordpress AJAX Load More Button

I just found this script https://github.com/tokmak/wp-load-more-ajax and I wanted to add it into my template.. All good, I added the script from functions.php into my template functions.php, copied the js file in my template folder and added that line from your_template.php into my template page where my posts are shown, the button appear but it doesnt work..
I have every file loaded, I checked but still it does nothing..
<?php if (have_posts()) : while ( have_posts() ) : the_post(); ?>
<div class="post-box">
<div style="float:left; margin-bottom: 10px; padding-right:20px;"> <?php if ( has_post_thumbnail() ) { echo get_the_post_thumbnail($post->ID); } else { echo my_post_thumbnail_html(); } ?> </div>
<div class="post-title"> <?php the_title(); ?> </div>
<div class="post-content"> <?php excerpt(10); ?> </div>
<br clear="all" />
</div>
<?php endwhile; ?>
<a class="load_more" data-nonce="<?php echo wp_create_nonce('load_posts') ?>" href="javascript:;">Load more</a>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; wp_reset_query(); ?>
This is my code.. I dont know what to do..
You must have a loaded content template.
Once, you create a template and add new page with this template. Your page link example: http://localhost.dev/?page_id=1453
This template containt only post loop. You can develop this template with $_GET params(numberposts, category etc.)
function loadMore(e, numberposts) {
$('.show-more', e).text('Loading...');
$('<div>').load('<?php echo get_bloginfo('template_url'); ?>/get_loop_template.php?s=' + numberposts, function(r) {
$('.list-articles .btn-getmore').remove();
$('.list-articles').append(r);
});
}
This function get your posts, remove current "getmore-buton" than insert posts and add new "getmore-buton".

Jquery slider not responsive - Wordpress

I'm putting together a responsive WordPress website but seem to be stuck when it comes to the slider. I think it's conflicting with something on my site, because the default theme (http://wordpress.org/themes/intuition) has a responsive slider.
This is the script I'm using:
//SLIDESHOW
jQuery(document).ready(function(){
//HOMEPAGE SLIDES
jQuery('.slider-slides').cycle({
speed: 1000,
timeout: 4000,
fx: 'scrollHorz',
next: '.slider-next',
prev: '.slider-prev',
pager: '.slider-pages',
pause: true,
pauseOnPagerHover: true,
containerResize: false,
slideResize: false,
fit: 1
});
jQuery('.slider-prev, .slider-next').click(function(){
jQuery('.slider-slides').cycle('pause');
});
jQuery(window).scroll(function(){
if(jQuery(document).scrollTop() > 500)
jQuery('#toplink').addClass('active');
else
jQuery('#toplink').removeClass('active');
});
});
function slide_resize(curr, next, opts, fwd) {
var ht = jQuery(this).height();
jQuery(this).parent().animate({height: ht});
}
I tried changing the resize options to true, but nothing changed. I tried adding width: 100% and height: 450px to the code as was suggested elsewhere, but that didn't change anything either.
I'm really at my wits end! If anyone has any suggestions, that'd be amazing. Thank you!
Here's my header.php:
<?php if(cpotheme_get_option('cpo_slider_always') == 1 || is_front_page()){ ?>
<?php $feature_args = array(
'post_type' => array('post', 'page'),
'meta_key' => 'page_featured',
'meta_value' => 'slider',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'ignore_sticky_posts' => 1,
'order' => 'ASC'); ?>
<?php $slider_posts = new WP_Query($feature_args); ?>
<?php if($slider_posts->post_count > 0): $slide_count = 0; ?>
<div id="slider" class="slider">
<ul class="slider-slides">
<?php while($slider_posts->have_posts()): $slider_posts->the_post(); ?>
<?php $slide_count++; ?>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), array(1500, 7000), false, ''); ?>
<?php $slide_position = get_post_meta(get_the_ID(), 'slide_position', true); ?>
<li id="slide_<?php echo $slide_count; ?>" class="slide slide-<?php echo $slide_position; ?>" style="background:url(<?php echo $image_url[0]; ?>) no-repeat center; background-size:cover;">
<div class="container">
<a class="slide-textbox" href="<?php the_permalink(); ?>">
<h2 class="slide-title"><?php the_title(); ?></h2>
<div class="slide-content"><?php the_excerpt(); ?></div>
</a>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if($slider_posts->post_count > 1): ?>
<div class='slider-prev'></div>
<div class='slider-next'></div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php }else{ ?>
<?php $header_image = get_header_image(); if($header_image != ''): ?>
<img src="<?php echo $header_image; ?>" class="header-image" />
<?php endif; ?>
You don't seem to have slide_resize bound to anything. The function exists, but it's not called anywhere. If by chance you do have it called somewhere else that you didn't show:
The "slider" itself and the "slides" themselves shrink to be mobile responsive (width at least). You just have the height of the slider to worry about, not the images themselves. Something like this should do the trick (sorry for syntax errors):
jQuery(window).on('resize', function(){
var currentWidth = jQuery('.slider-slides .container').width();
var newHeight = currentWidth / 2.444; //2.444 is your 1100/450 from the full size slider
('.slider-slides .container').height(newHeight); //Container height
jQuery('.slider-slides .slide').height(newHeight); // Slide wrap's height
});

Bring a WordPress post dynamically

I've been trying to make this feature work for many days now and it's driving me nuts!
I have a single page theme in WP and in one of them there is a div on the left with a list of the posts in the site and on the right, a div that should display the content of the clicked post.
I found this question and followed up the linked tutorial and was partially successful.
I managed to bring the content dinamically, and all I want is being displayed but it seems the order of the tasks are wrong. Heres how it's acting:
I click on the link.
the current content goes away.
the loading span appears correctely.
the SAME content fades in.
after 1 second or so the current content is replaced with the new content and the address bar does not change at all.
Here's the code I have:
atracoes.js
$(document).ready(function() {
var hash = window.location.hash.substr(1);
var href = $('.controle nav li a').each(function(){
var href = $(this).attr('href');
if(hash==href.substr(0,href)){
var aCarregar = hash+'.html #atr-conteudo';
$('#atr-conteudo').load(aCarregar)
}
});
$('.controle nav li a').click(function() {
var aCarregar = $(this).attr('href')+' #atr-conteudo';
$('#atr-conteudo').hide('fast',carregarConteudo);
$('#carregando').remove();
$('#atracoes').append('<span id="carregando">Carregando...</span>');
$('#carregando').fadeIn('normal');
window.location.hash = $(this).attr('href').substr(0,$(this).attr('href'));
function carregarConteudo () {
$('#atr-conteudo').load(aCarregar,'',mostrarNovoConteudo());
}
function mostrarNovoConteudo () {
$('#atr-conteudo').show('normal',esconderCarregando());
}
function esconderCarregando () {
$('#carregando').fadeOut('normal');
}
return false;
});
});
index.php (the dynamic content part)
<div class="main" id="atracoes">
<div class="controle">
<nav>
<?php
$args = array( 'posts_per_page' => 20);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</nav>
</div>
<div id="atr-conteudo">
<?php the_post_thumbnail(); ?>
<div id="atr-texto">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
single.php (the part I'm plucking with ajax)
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- post thumbnail -->
<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); // Fullsize image for the single post ?>
</a>
<?php endif; ?>
<!-- /post thumbnail -->
<div id="atr-texto">
<!-- post title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /post title -->
<?php the_content(); // Dynamic Content ?>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
</div>
</article>
You're calling the functions before you pass them to jQuery to execute, instead of allowing jQuery to execute them:
function carregarConteudo () {
$('#atr-conteudo').load(aCarregar,'',mostrarNovoConteudo);
}
function mostrarNovoConteudo () {
$('#atr-conteudo').show('normal',esconderCarregando);
}
(Notice they no longer have () after the function names)

Categories

Resources