Is it possible to modify this code to return the categories in reverse order (i.e Highest ID first?) It's currently displaying the output with the lowest ID first. Many thanks in advance!
$imageCols = JRequest::getVar("imageCols");
$thumbWidth = JRequest::getVar("categoryThumbWidth", 4);
$columnWidth = floor(100 / $imageCols);
$document =JFactory::getDocument();
$document->addStyleSheet('components/com_groovygallery/css/groovygallery.css');
$document->addScript( JURI::base().'components/com_groovygallery/js/modernizr.custom.84782.js' );
$document->addScript( JURI::base().'components/com_groovygallery/js/masonry.pkgd.min.js' );
<?php if($this->params->get('show_page_heading')){?><h2><?php echo $this->params->get('page_heading');?></h2><?php } ?>
<div id="groovyGalleryCats" data-masonry-options='{ "columnWidth": 200, "itemSelector": ".groovyGalleryCat" }'>
<?php
foreach($this->categories as $key=>$category){
$params = json_decode($category->params);
$catImage = JURI::base().$params->image;
?>
<a class="groovyGalleryCat" href="<?php echo JRoute::_('index.php?option=com_groovygallery&view=images&filter_category='.$category->id); ?>" >
<?php if($params->image){ ?><img src="<?php echo JURI::base().'components/com_groovygallery/timthumb.php?src='. $catImage . '&zc=2&w='.$thumbWidth;?>" alt="<?php echo $category->title;?>" /> <?php } ?>
<?php echo $category->title;?>
<?php //echo $category->description;?>
</a>
<?php
}
?>
</div>
<style>
a.groovyGalleryCat {width:<?php echo $columnWidth;?>%;}
</style>
<script src="<?php echo JURI::base().'components/com_groovygallery/js/init-cat.js'; ?>" defer="defer"></script>
You can reorder the array like this (supposing that the id property is public...):
uasort($this->categories, function($a, $b) {
return $a->id > $b->id ? -1 : 1;
});
Related
I didn't build this PHP file, but I am trying to figure out why it is giving me errors. If the "Block layout mode" ACF variable is set to "Modal" then the block works, but gives me this error. "Undefined variable $vids on line 176". If I select the "Embed & Thumbnail" mode, then none of the block is displayed and I get this error. "Call to undefined function vc_embed_layout() on line 252". I can't pin down why this is happening, and I haven't been able to find any unclosed tags or anything in my php file. This is the file in question, and I've marked the lines that trigger errors.
<?php
global $theme_text_domain;
$block_class_identifier = 'blk__vidcol';
// default variables
$block_id = $top_class = $top_style = $inner_class = $inner_style = '';
// check for custom id
$block_id .= BLOCK::set_id( get_sub_field('content_block_id') );
// set block class, $i comes from FLEX class loop and represents the block order on a page
$top_class .= BLOCK::set_standard_classes( $i );
$top_class .= BLOCK::set_custom_classes( get_sub_field('content_block_classes') );
// check for top padding adjustment
$top_class .= BLOCK::set_padding_class( get_sub_field('block_padding_adjustment') );
$top_style .= BLOCK::set_padding_style( get_sub_field('block_padding_adjustment'), get_sub_field('custom_top_padding'), get_sub_field('custom_bottom_padding'));
// check for custom color theme
$top_style .= BLOCK::set_colors( get_sub_field('color_theme'), get_sub_field('text_color'), get_sub_field('custom_background_color') );
// check for width settings
$inner_class .= BLOCK::set_width_class( get_sub_field('content_width') );
$inner_style .= BLOCK::set_width_style( get_sub_field('content_width'), get_sub_field('custom_maximum_content_width') );
$block_assets = get_stylesheet_directory_uri() . '/inc/blocks/' . basename(__FILE__, '.php') . '/assets/';
/*
CUSTOM BLOCK SPECIFIC SETUP
*/
$layout = get_sub_field('block_layout_mode');
$top_class .= ' layout-' . $layout;
// title color
$h2_style = (get_sub_field('color_theme') == 'custom') ? ' color: ' . get_sub_field('custom_title_color') . ';' : '';
/**
* outputs code for the modal version of the video collection block
* #return html output
*/
if (!function_exists('vc_modal_layout')) {
function vc_modal_layout() {
global $block_class_identifier;
$cta_label = get_sub_field('cta_label');
$counter = 0;
$cta = array(
'style' => get_sub_field('cta_style'),
'button_color' => get_sub_field('button_color'),
'button_text_color' => get_sub_field('button_color')
);
$cta_class = BLOCK::get_cta_style( $cta, '.' . $block_class_identifier . ' .button');
?>
<?php // list section ?>
<?php if( have_rows('videos') ): ?>
<div class="vid-container">
<?php while ( have_rows('videos') ) : the_row(); ?>
<?php if ($counter == 0): ?>
<div class="vid-featured">
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
<a class="vid_overlay" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_0" href="javascript:;"></a>
</div>
<!-- modal -->
<div id="<?php echo $block_class_identifier; ?>_vid_0" class="modal video-modal" style="display: none;">
<div class="modal-inner">
<h3><?php echo get_sub_field('video_title'); ?></h3>
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
<div class="vid-list">
<?php else: ?>
<div class="vid-item">
<div class="item-btn">
<a class="button <?php echo $cta_class; ?>" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" href="javascript:;"><?php echo $cta_label; ?></a>
</div>
<div class="item-title"><?php echo get_sub_field('video_title'); ?></div>
</div>
<!-- modal -->
<div id="<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" class="modal video-modal" style="display: none;">
<div class="modal-inner">
<h3><?php echo get_sub_field('video_title'); ?></h3>
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
<?php endif; ?>
<?php $counter++; ?>
<?php endwhile; ?>
</div>
</div>
<?php else: ?>
No videos defined.
<?php endif; ?>
<?
}
}
/**
* outputs code for the embed version of the video collection block
* #return html output
*/
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids, $i) {
?>
<?php if ( $vids ): ?> **this is line 176**
<div class="vid-container2">
<h2 class="vid-title"><?php echo $vids[0]['video_title']; ?></h2>
<div class="vid-container2-inner">
<div class="vid-embed">
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo $vids[0]['youtube_embed_code']; ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen onload="this.style.visibility = 'visible';"></iframe>
</div>
</div>
<div class="vid-thumbnails">
<?php foreach ($vids as $key => $vid): ?>
<?php $wrap_class = ($key == 0) ? ' tn-hide' : ''; ?>
<div class="tn-wrapper <?php echo $wrap_class; ?>" data-embed="<?php echo $vid['youtube_embed_code']; ?>" data-title="<?php echo $vid['video_title']; ?>">
<img src="https://img.youtube.com/vi/<?php echo $vid['youtube_embed_code']; ?>/mqdefault.jpg" alt="">
</div>
<?php endforeach; ?>
</div>
</div>
<script>
var vid_swap = function() {
$('.block_<?php echo $i; ?> .vid-embed').css({opacity: 1});
$('.block_<?php echo $i; ?> .tn-wrapper').on('click', function(e) {
var $this = $(this);
var embed_id = $this.attr('data-embed');
var title = $this.attr('data-title');
console.log('embed = ' + embed_id);
$('.block_<?php echo $i; ?> .vid-title').animate({opacity: 0}, 100);
$('.block_<?php echo $i; ?> .vid-embed').animate({opacity: 0}, 1000, function() {
$('.block_<?php echo $i; ?> .vid-embed, .block_<?php echo $i; ?> .vid-title').css('visiblity','hidden');
$('.block_<?php echo $i; ?> .vid-title').html( title );
$('.block_<?php echo $i; ?> .vid-embed iframe').attr('src', 'https://www.youtube.com/embed/' + embed_id);
$('.block_<?php echo $i; ?> .tn-wrapper.tn-hide').removeClass('tn-hide');
$this.addClass('tn-hide');
$('.block_<?php echo $i; ?> .vid-title').css('visiblity','visible').delay(500).animate({opacity: 1}, 500);
$('.block_<?php echo $i; ?> .vid-embed').delay(500).animate({opacity: 1}, 1000);
});
});
}
jQuery(document).on('block_init', vid_swap);
</script>
</div>
<?php else: ?>
No videos defined.
<?php endif; ?>
<?php
}
}
?>
<section id="<?php echo $block_id; ?>" class="<?php echo $block_class_identifier; ?> <?php echo $top_class; ?>" style="<?php echo $top_style; ?>">
<div class="inner <?php echo $inner_class; ?>" style="<?php echo $inner_style; ?>">
<?php // title section ?>
<?php if (get_sub_field('title')): ?>
<h2 style="<?php echo $h2_style; ?>"><?php echo get_sub_field('title'); ?></h2>
<?php endif; ?>
<?php $vids = get_sub_field('videos'); ?>
<?php if ($layout == 'modal') vc_modal_layout($vids); ?>
<?php if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**
</div>
</section>
You're not accepting any arguments in vc_modal_layout() function declaration.
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout() {
needs to have parameters in order to accept arguments.
something like this is what you're looking for:
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids=[], $i=0) {
And it's the same idea with vc_modal_layout, the function needs to be able to accept the variables from outside it.
When we call a function, the function needs to be able to accept all arguments passed to it. And all variables we use need to be defined, if there's a risk of them being undefined or empty arrays, we can check them with if(!empty($var)) or if(isset($var))
An alternative to passing the variable to the function is to access it as a global. But it's generally better to pass the variable to the function because when we pass the variable to a function in PHP it works with a copy of the variable's value inside the function. When we do something like this:
$var = 10;
function doIt(){
global $var;
$var++;
}
doit();
echo $var;
we could overcomplicate things..
let's have a closer look here:
if ($layout == 'modal') vc_modal_layout($vids);
if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**
function vc_modal_layout(){ ... }
// should be:
function vc_modal_layout($vids){ ... }
function vc_embed_layout($vids, $i)
// looks good.
Aside from that, nothing stands out as really unusual & I'd want to have a look at the actual site to tinker with this problem further.
I am new to Magento.
How can I getMediaGalleryImages within a sort order range in Magento? I am new to magento and would like to get all images for a product within a sort oorder range of say 10-20.
This is what I have so far:
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())- >getMediaGalleryImages();?>();?><?php if($_images){?><?php $i=0; foreach($_images as $_image) if ($i++ < 8) { $i++;?>
Any help is welcome!
Thanks for the answers but, I'm not quite getting it I'm afraid, I want to display thumbs under my products on the product list page. I'm sure the code you have given is correct but I am not implementing it correctly. Here's the full code for that I have at the moment:
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())- >getMediaGalleryImages();?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image){ $i++; if($i>=10 && $i<=20);?>
<a href="#">
<img src="<?php echo $helpImg->getImg($_product, $bigImageWidth, $bigImageHeight, 'image', $_image->getFile()); ?>" class="cloud-zoom-gallery lightbox-group"
title="<?php echo $this->escapeHtml($_image->getLabel()); ?>" width="25" height="25" alt="<? =$this->htmlEscape($_image->getLabel());?>" title="<?=$this->htmlEscape($_image->getLabel());?>" />
you need to change code
$_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages();
$i=1;
foreach($_images as $_image){
if($i > 10 && $i < 20){
echo $_image['url'];
}
$i++;
}
You can do it by doing following code.
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image){ $i++; if($i>=10 && $i<=20): ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(108,90); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" /><?php endif; } ?>
<?php } ?>
Change the range whatever you want.
This is example for product gallery images order from 10 to 20.
I have the follow foreach loop:
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=1;
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count++; }; ?>
I need help getting the count to reverse. The output I am looking for is as follows:
<div id="slide4"></div>
<div id="slide3"></div>
<div id="slide2"></div>
<div id="slide1"></div>
Any help with this would be great.
Change $count=1; to $count = sizeof($arrayreversed);
and $count++; to $count--;
First of all get the amount of keys in the array and assign it as $count, like this:
$count = count($arrayreversed);
Then replace the $count++ with $count-- and you're all set.
How about using the size of the array as your initial count and then subtracting 1 each iteration?
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=count($arrayreversed);
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count--; }; ?>
I'm using joomla! to output some extra fields into an article. The fields are a list of images (max of 10) which are displayed using backslider jquery plugin.
Here is the code i've used which works:
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
$img1 = $this->item->extrafields['image_1'];
$img2 = $this->item->extrafields['image_2'];
$img3 = $this->item->extrafields['image_3'];
$img4 = $this->item->extrafields['image_4'];
$img5 = $this->item->extrafields['image_5'];
$img6 = $this->item->extrafields['image_6'];
$img7 = $this->item->extrafields['image_7'];
$img8 = $this->item->extrafields['image_8'];
$img9 = $this->item->extrafields['image_9'];
$img10 = $this->item->extrafields['image_10'];
?>
<?php if($img1) { ?>
<li><img src="<?php echo $img1; ?>"></li>
<?php } ?>
<?php if($img2) { ?>
<li><img src="<?php echo $img2; ?>"></li>
<?php } ?>
<?php if($img3) { ?>
<li><img src="<?php echo $img3; ?>"></li>
<?php } ?>
<?php if($img4) { ?>
<li><img src="<?php echo $img4; ?>"></li>
<?php } ?>
<?php if($img5) { ?>
<li><img src="<?php echo $img5; ?>"></li>
<?php } ?>
<?php if($img6) { ?>
<li><img src="<?php echo $img6; ?>"></li>
<?php } ?>
<?php if($img7) { ?>
<li><img src="<?php echo $img7; ?>"></li>
<?php } ?>
<?php if($img8) { ?>
<li><img src="<?php echo $img8; ?>"></li>
<?php } ?>
<?php if($img9) { ?>
<li><img src="<?php echo $img9; ?>"></li>
<?php } ?>
<?php if($img10) { ?>
<li><img src="<?php echo $img10; ?>"></li>
<?php } ?>
</ul>
</div>
I'm not a php expert but is there a better way of optimising this code, I'm thinking maybe putting the $img variables into an array and using a foreach loop to output each list item?
A little help wouldn't go a miss :)
Just iterate over them and use the index to reference the array indices:
for ($i = 1; $i <= 10; ++$i) {
if (!empty($this->item->extrafields["image_$i"])) {
echo '<li><img src="', htmlspecialchars($this->item->extrafields["image_$i"]), '"></li>';
}
}
Assuming there are up to 10 items to investigate.
The solution below will avoid evaluating strings, cache's everything you can, to keep your application running smoothly, and keeps your code nice and tidy.
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
// Generate length of our image array / store known length of array
$images = 10;
// Loop through images
for( $i = 1; $i <= $images; $i++ ) {
// Store it for optimization sake.
$field = $this->item->extrafields['image_' . $i];
// Check it's not empty
if( !empty( $field ) ) {
// If not, print to browser
printf(
'<li><img src="%s" alt=""></li>',
htmlspecialchars($field)
);
}
}
?>
</ul>
</div>
I think this code will do same thing.
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
for($i = 1; $i <= 10; $i++)
{
$img = $this->item->extrafields['image_'.$i]
if($img) { ?>
<li><img src="<?php echo $img; ?>"></li>
<?php }
}
?>
</ul>
</div>
Im using this to display a list of thumnails called from my cms:
<?php if($gallery_images) { ?>
<?php
$slide_page = 1;
foreach($gallery_images as $count => $image) { ?>
<li><a href="<?php echo $image->getResizedImage(); ?>" rel="example1" title="********"><img width="125" height="80" src="<?php echo $image->getThumbnailImage()
?>" /></a></li>
<?php if(($count+1) % 3 == 0) {
$slide_page += 1;
?>
It calls images from within my CMS and displays them in groups of 3, with some added jquery to scroll through the sets.
What im trying to do is merge this with my videos within the same list.
The video code is as follows:
<?php foreach($videos as $count => $video) { ?>
<img src="{thumbnail}" />Video A
<?php } ?>
Ive tried using the array_merge function but seem to be having difficulties any help would be greatly appreciated
It's easy:
foreach (array_merge($gallery_images, $videos) as $count => $value) { }
You may also want to look at array_chunk().
Update:
<? foreach (array_merge($images, $videos) as $key => $value): ?>
<? if (is_object($value) === true): ?>
<? if (method_exists($value, 'getLocation') === true): ?>
<img src="{thumbnail}" />Video A
<? elseif (method_exists($value, 'getResizedImage') === true): ?>
<img width="125" height="80" src="<?= $image->getThumbnailImage(); ?>" />
<? endif; ?>
<? endif; ?>
<? endforeach; ?>