I am using the NextGen gallery WordPress plugin for a site. In my gallery.php template I want to retrieve the number of images for each gallery displayed in the loop. I cannot figure out a way to get the data and print it under the thumbnail of each gallery that's called in gallery.php
Here is where I want to insert the gallery image count and print it:
<a rel="prettyPhoto" href="<?php echo $image->imageURL ?>"
<?php $image->thumbcode ?>><span>view</span></a> <?php echo $total_images; ?> pictures
Anybody have any tips?
Thanks,
Ian
I used this little hack to get my gallery id from the shortcode I pasted inside my post ([nggallery=1])
<?php
$id = get_the_ID();//Get the id of the specific post (inside the loop)
$string = get_the_content();//You get the whole post content where in the end of it lies your shortcode (for example [nggallery=1])
if(preg_match_all('/=(.*?)\]/s',$string,$match)) {
$finalstring=$match[1][0];
}// get the id only (gets all chars after '=' and before ']')
global $wpdb;
$total_attachments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures WHERE galleryid=$finalstring" ); // Voila!
?>
Hope my solution can work for you.
This is the code you need to count the images:
$global $wpdb;
$images = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures") );
$galleries = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggallery") );
$albums = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggalbum") );
The answer is you can't.
You can count the keys in the $images variable but it will return the wrong number.
You can retrieve the total number of all the images, or all the galleries which is useless.
You can even use a counter as you cycle through the array to try to count how many images are in your gallery, but you'll still get the wrong number.
You can try to use the global variables tthere by default: images->total, which are empty and undefined and don't even exist in the $images or $current object.
The code displays a ("Picture 3 of 7)" type display. If you use it in the imagebrowser.php template, it works. If you paste this exact same code in the gallery-carousel.php template. It will not work. Now you might think switching to $current->total would work, since it inexplicably does for the rest of the variables when using gallery_carousel, but no, it does not. The answer is you have to pull the info from the database directly.
<?php _e('Picture', 'nggallery') ?> <?php echo $image->number ?> <?php _e('of', 'nggallery')?> <?php echo $image->total ?>
It is a sinister, sinister plugin.
If you can't count the images on a gallery, you can count the tags on html block.
On my theme, I get the gallery ID from an custom field and echo the gallery inside the template. So, it's something like that:
<?php
$myGalleryId = 1; // example
$displayImages = 0; // the number of images you want to display from gallery. 0 = all images
$newnggShortcodes = new NextGEN_Shortcodes;
$htmlGallery = str_get_html($newnggShortcodes->show_gallery( array("id"=>$myGalleryId,"images"=>$displayImages,"template"=>"popular") ));
$countImg = count($htmlGallery->find('img')); // number of total <img> tags inside the gallery
$str = $htmlGallery;
?>
<!-- now the html -->
<div id="myGallery">
<?php echo $str; ?>
</div>
<span>Total images: <?php echo $countImg; ?></span>
Hope it helps.
Related
I've add module "categories" to homepage, and now I see categories list with numer of products on homepage but can't see category thumbs.
in file: /public_html/catalog/view/theme/MY-THEME/template/module/category.tpl I found around line 10:
<?php echo $category['name']; ?>
and I think sommewhere there I also should add something like: category['thumb'] to display image before category name but do I have to add something in controller files, I soppouse yes but need help here.
I thing i should define $category['thumb'] in controller but how to do this?
You're correct in saying that you need to edit the controller file for this module, you need to add the model that gets the image for that category as well as get the image for any child categories. So there are two edits you need to make, first to the controller file
Open the catalog/controller/module/category.php & find this line:
$this->load->model('catalog/product');
Now you need to add the model so that you can get the image data from the DB, add this line below it:
$this->load->model('tool/image');
Now find this line, it will be in the foreach loop:
$children = $this->model_catalog_category>getCategories($category['category_id']);
Add the following above the line:
$category_info = $this->model_catalog_category>getCategory($category['category_id']);
if ($category_info['image']) {
$image = $category_info['image'];
} else {
$image = '';
}
You also need to do this for the child categories, find this line in the next foreach loop:
$product_total = $this->model_catalog_product->getTotalProducts($data);
Add this directly below it:
$child_info = $this->model_catalog_category>getCategory($child['category_id']);
if ($child_info['image']) {
$child_image = $child_info['image'];
} else {
$child_image = '';
}
At the moment we now have two variables that contain the image paths: $image and $child_image, these need to be passed to the template file, you can do so by editing the two arrays that are created. We'll do the child array first as that is almost directly below the last edit you made. Find this line in the children_data array:
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
Add a comma to the end of the line and paste this line directly below it:
'image' => $child_image
Now we add the image to the $category array, find this line and add a comma to the end as with the last step:
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
The next step is obvious now, add the following line directly below it:
'image' => $image
Now you should note that the template file can access a new variable $image when in the foreach loops. I'll leave the rest to you as you'd know how and where you'd like to display the image. The code below will generate an image if it's used in the foreach loop of either the Category:
<img src="<?php echo $category['image']; ?>
Or this can be used for a child category:
<img src="<?php echo $child['image']; ?>
On view.phtml, i am trying to get the custom block "console" to display if the value of the attribute platform is either "xbox", "playstation" or "nintendo".
I got the code working for xbox, but how can i solve it so that the block is displayed it the value instead is playstation or nintendo?
Br,
Tobias
<?php if ($_product->getAttributeText('platform') == "xbox"): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('console')->toHtml() ?><?php endif; ?>
Do you mean you want the same block for any of the consoles? I suggest a switch statement
Replace the if statement you wrote with this:
switch($_product->getAttributeText('platform')){
case "xbox" :
case "nintendo" :
case "playstation" :
echo $this->getLayout()->createBlock('cms/block')->setBlockId('console')->toHtml();
break;
default :
//here you can put a default other block or show nothing or whatever you want to do if the product is not a console
}
Now you can add more consoles by adding more case "other console" : statements.
There are other ways. You could create an array of all consoles from the attribute values and then use inArray(); - that might be better for the general case if your client adds more consoles to the attribute via the Magento admin.
**EDITED ** following the comment below
If the attribute 'platform' is multiselect then $_product->getAttributeText('platform') will be string if one item is selected but if there are multiple items selected it will be an array. So you need to handle a single variable that can be string or array. Here we will convert string to array and use PHP's handy array_intersect() function.
I suggest:
$platformSelection = $_product->getAttributeText('platform');
if (is_string($platformSelection))
{
//make it an array
$platformSelection = array($platformSelection); //type casting of a sort
}
//$platformSelection is now an array so:
//somehow know what the names of the consoles are:
$consoles = array('xbox','nintendo','playstation');
if(array_intersect($consoles,$platformSelection)===array())
{
//then there are no consoles selected
//here you can put a default other block or show nothing or whatever you want to do if the product is not a console
}
else
{
//there are consoles selected
echo $this->getLayout()->createBlock('cms/block')->setBlockId('console')->toHtml();
}
Note that the array_intersect() function compares the array elements strictly that is === so it is case sensitive and the function returns an empty array array() if there is no intersection.
If you want the same block for either of those 3 values, this should do it:
<?php
$productAttribute = $_product->getAttributeText('platform');
if ($productAttribute == "xbox" || $productAttribute == "playstation" || $productAttribute == "nintendo"): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('console')->toHtml() ?><?php endif; ?>
If you want different blocks depending on the value, this should do it:
<?php
$productAttribute = $_product->getAttributeText('platform');
switch ($productAttribute){
case "xbox":
echo $this->getLayout()->createBlock('cms/block')->setBlockId('xbox')->toHtml();
break;
case "playstation":
echo $this->getLayout()->createBlock('cms/block')->setBlockId('playstation')->toHtml();
break;
case "nintendo":
echo $this->getLayout()->createBlock('cms/block')->setBlockId('nintendo')->toHtml();
break;
}
?>
I'm coding a website for a fabric shop that sells by the yard. They sell in decimal quanities which I have figured out how to enable. However in the front end (list and view) it doesn't show the decimal amount on the end. For instance, if there is 24.75 yds in stock, it displays that there are 24 yds in stock.
What would I need to do to fix that?
Thanks!
<?php $qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); echo $qty; ?>
Can you remove the (int) casting and try that?
<?php
$qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo number_format((float) $qty, 2, '.', '');
?>
I've been looking everywhere but haven't found the answer to the question: How can I use my own thumbnail per feed in a multifeed page? If I've overlooked the answer it would be due to the wrong search keys for which i apologize in advance.
I've got the following code so far (which is not much):
<?php
include_once('autoloader.php');
include_once('idn/idna_convert.class.php');
date_default_timezone_set('Europe/Amsterdam');
setlocale(LC_TIME, 'nld_nld');
$feed = new SimplePie();
$urls = (array(
'http://myfeedurla.com' => 'descriptiona',
'http://myfeedurlb.com' => 'descriptionb',
'http://myfeedurlc.com' => 'descriptionc'
));
$feed->set_feed_url(array_keys($urls)); // this should get parse only the URLS
$feed->set_cache_location('cache/');
$feed->init();
$feed->handle_content_type();
?>
// As from here I'm using the standard simplepie code to show multiple feeds and order them by day
<?php
// Set up some variables we'll use.
$stored_date = '';
$list_open = false;
// Go through all of the items in the feed
foreach ($feed->get_items() as $item)
{
// What is the date of the current feed item?
$item_date = $item->get_date('M jS');
// Is the item's date the same as what is already stored?
// - Yes? Don't display it again because we've already displayed it for this date.
// - No? So we have something different. We should display that.
if ($stored_date != $item_date)
{
// If there was already a list open from a previous iteration of the loop, close it
if ($list_open)
{
echo '</ol>' . "\r\n";
}
// Since they're different, let's replace the old stored date with the new one
$stored_date = $item_date;
// Display it on the page, and start a new list
echo '<h1>' . $item->get_local_date('%A %d %B %Y') . '</h1><hr>' . "\r\n";
echo '<ol>' . "\r\n";
// Let the next loop know that a list is already open, so that it will know to close it.
$list_open = true;
}
// Display the feed item however you want...
echo '<li>' . $item->get_local_date('%H:%M') . ' | <h4>' . $item->get_title() . '</h4></li>' . "\r\n";
}
?>
Somewhere in the HTML I want to add the 'img scr=" . . "' etc. And there it should only refer to an image by the name 'descriptiona'.png respectively 'descriptionb'.png etc, per feed. Is this possible? And if yes, how?
If I should be more clear, pls don't hesitate to ask. Thanks in advance for the help!
Best regards,
Ok, so far I have one solution :) I use the title of the respective feeds in the img filename. I've added the following code:
<img src="images/' . $item->get_feed()->get_title() . '.png" width="12" height="12" />
Now I only hope that feeds won't use / or something in their titles... If someone has a better way of doing this, eg. by being able to add a personal title per feed, that would be very welcome.
its a silly question but I'm really bad in escaping quotes :/.
I'm using the table class to generate a table of products, and in that table each row has an image to be displayed for that product. The images are stored using their product_id as a name with an appended "_x" because there's more than one image per product, so an example of an image name is 193_1.
This is how I'm generating my rows for the table:
$table_row = array();
foreach ($r->result() as $p)
{
$table_row = NULL;
$table_row[] = "<img src='http://localhost/CI/photos/$p->product_id\"1\".jpg' height='150' width='150'/>";
$table_row[] = $p->product_id;
$table_row[] = $p->title;
$table_row[] = $p->description;
$table_row[] = $p->price;
$table_row[] = $p->stock;
$this->table->add_row($table_row);
}
But the image won't show, even when I've escaped the '1'. I only want to display the first image as that is the main one, so that's why I've hard-coded the number. Could someone please help? I've tried multiple ways i.e.
"<img src='http://localhost/CI/photos/$p->product_id '1' .jpg' height='150' width='150'/>"
"<img src='http://localhost/CodeIgniter/photos/<?php echo $p->product_id; ?>1.jpg' height='150' width='150'/>";
But the image still doesn't show. The table is being generated in my Controller which is a PHP file so I don't really think I need to use php tags
"<img src='http://localhost/CodeIgniter/images/" . $product->product_id . "_1.jpg' height='100' width='100'/>";