How do I get a galleries cover image in modx revolution? - php

How do I get a galleries cover image in modx revolution?
Currently I'm calling the gallery by ID and limiting it to one image ~ however, this does not garantee that the image will be the cover image.
this is what I am currently doing.
[[!Gallery? &album=`[[!+tv.vehiclegallery]]` &toPlaceholder=`gallery` &thumbTpl=`homepageVehicleThumbTpl` &limit=`1`]]
[[+gallery]]

The answer is way more simple! I was looking at this for hours and couldnt figure it out, then it hit me. The cover of every album is always called "cover.jpg".
And it will be stored in the assets folder with the corresponding Album-ID.
Path abstract:
assets/gallery/[album-id]/cover.jpg
For example:
assets/gallery/1/cover.jpg
So as long as you have the Album-ID you can get the cover.
Getting the Album-ID
If you want the Album-ID heres one way:
Create a Template Variable called gallery_album with the input type galleryalbumlist.
Make it accessible to your preferred template.
Open up, or create a resource with that template.
The Template Variable will be in the Template Variable Tab as a Dropdown containing
all the previously created albums.
Choose an album from the dropdownlist
when you know call [[+gallery_album]] in the template, it will output the Album-ID
Code example: <img src="assets/gallery/[[+gallery_album]]/cover.jpg"
Link to the modx extra Gallery:
https://docs.modx.com/current/en/extras/gallery/gallery/index

I would up doing this in a snippet instead:
<?php
//getgalleryAlbumCover
$output = '';
$sql = "select * from modx_gallery_albums mga where mga.id = $id";
$album = $modx->query($sql);
if (!is_object($album)) {return;}
$data = $album->fetch(PDO::FETCH_ASSOC);
// just in case ;)
$modx->toPlaceholders($data);
$output = $data['cover_filename'];
return $output;

Related

How to change the properties (title, caption) of images in a wordpress page with functions.php?

I have a wordpress website which is in both english and french, meaning that for every english page, there's its french counterpart with the same content. Until now, when I wanted to add something to it like a slideshow, I'd do two of them and add them on their respective pages but I have a problem with galleries and solo images as their caption and title are used directly in those cases. I don't want to upload each image twice with different properties so I wanted to add a function in php that switches the language for me.
My idea was to put tags in the images caption to have something like that :
[EN] EN description
[FR] FR description
And have the function get this string and work it to remove what has to be removed for the current page. My problem is that I don't really know php and even less the WordPress api of which I struggle to understand how it works, what I get when I call a function or even what it's talking about in the functions description.
So my question is this: Is there a way to get all the images of a page and edit their properties ? If yes, how ?
I tried to search for clues on the internet and found this code that I wanted to use as a base but I'm not sure it's working:
$PageID = get_the_ID();
$Post = get_post( $PageID );
$Content = $Post->post_content;
$Regex = '/src="([^"]*)"/';
preg_match_all( $Regex, $Content, $Matches );
foreach( $Matches as $Image ):
echo '<img src="'.$Image.'" alt="'.$Post->post_title.'" title="'.$Post->post_title.'">';
endforeach;
Maybe I'm missing something but when I try to print $Matches or $Image in the console, all I get is "Array" and it gives me a broken image with my page title on its side if I try to run it.
Thank you for your time.
If you want to change HTML markup for each image (ex. add custom attributes to each <img>), maybe this is a good thread: Change html structure of all img tags in Wordpress # wordpress.stackexchange.com
If you want to change how WP outputs the caption for an image, which is inserted based on a shortcode in the editor, then this WP codex page should help: Plugin API/Filter Reference/img caption shortcode # codex.wordpress.org
There is an example in the second link. You should be able to get the image caption in a variable (see $attr['caption']) - and then you should filter it (ex. remove the [EN] description). But you need to figure how to access the language of the page in that function.

e-commerce href management

I have a question regarding e-commerce websites. Lets assume there is an e-commerce website that sells products for women, men, kids. For each of these categories it sells clothes, shoes ---etc. When a user clicks on women or men or whatever category, the user should be directed to a page showing the list of products corresponding to that category. Lets assume that the website have the same layout. For example the navbars, the squares where products will be displayed are the same. the only difference is the content it self. My question does large e-commerce websites have a separate html file for each product or they have some a template html file where the design and layout is fixed and based on link clicked the content of html is changed. Having a separate html file would be very cumbersome. If having a template html file is the case, how does the name of the link in browser change when different products are clicked is it related to .htaccess.
Thanks.
Yes .htaccess is important in this case. Try to learn how to parse url, and make some simple MVC applications.
There are many methods to doing this. The main answer to your question is that you need to parse the url. I'll go over a couple here.
1) PHP looks like it would be the easiest solution if you're connecting to a mysql database with products. You can create a php file with the categories as parameters, so each link could look like products.php?category=shoes
Then you can parse that url with $_GET in your code.
$category = $_GET['category'];
Then, in your php file, you can display specific content based on the category like so:
if ($_GET['category'] = 'shoes'){
//then enter your logic here
}
elseif ($_GET['category'] = 'books'){
//then enter your logic here
}
Here's some more info on the PHP GET: http://w3schools.sinsixx.com/php/php_get.asp.htm
You can do this via javascript by parsing the href
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var dynamicContent = getParameterByName('dc');
and then writing the logic to correctly create elements that fit your requirements through javascript.
More details: http://jennamolby.com/how-to-display-dynamic-content-on-a-page-using-url-parameters/
This should help you move forward to understanding how to achieve your concept hopefully!

How can I get the url of featured image from Rainlab blog plugin?

I need to know the url of a featured image attached in a blog post with Rainlab blog plugin in October CMS.
The image is saved in a directory something like ".../storage/app/uploads/public/59f/112"
I need to know it in the moment that I save the post because I want to get the url to save it in another table that it can access from another php file or system ( in Android App for example ), but I can't do it.
I try with (in FormController.php) find the post:
$modelP = Post::find($this->controller->vars['formModel']['id']);
$featuredImage = $modelP->featured_image->getPath();
But doesn't work, I get the blog object but it says that featured_image is not a variable.
In the table system_file only I can get the name of the file (disk_name) but not the entire url and I don't know in what directory it is saved.
Can anyone help me?
If you are using rain lab plugin then we do have some solution
first its featured_images not featured_image
It will going to return you a collection of images so if you need first image of it, then you replace code like
$modelP = Post::find($this->controller->vars['formModel']['id']);
$featuredImage = $modelP->featured_images->first()->getPath();
$featuredImage this is full path of image
any other confusion please comment, happy coding :)
its not singular its plural like
$featuredImage = $modelP->featured_images()->first();
Edit: use First

Select Random Page Image with ProcessWire

I'm just starting out using the ProcessWire system and really enjoying it.
On my Home Page, I would like to display an image from a random page. The page can be ANY page as long as the it is the child of the parent page with ID '1010'.
Is it possible, and if so, how do I achieve this?
My current code for showing the home page image is this:
if($page->image) echo "<img src='{$page->image->url}'>"; however, I'd like to select a random image from any of the children pages of the above parent ID.
I found this, but wasn't sure whether it would be of any use.
Many thanks for any pointers :-)
You should try something like this in your template's code (assuming that your image field is called image):
/* Find all children of page with ID 1010 that include an image */
$allChildPages = $pages->find('parent=1010,image.count>0');
/* Select a page from all children in the PageArray randomly */
$randomChildPage = $allChildPages->getRandom();
if ($randomChildPage->image) {
echo "<img src='{$randomChildPage->image->url}'>";
}
Have a look at the relevant code:
$pages->find() returns a collection of Pages (matching the Selector) as a PageArray (that extends the WireArray class).
$anyWireArray->getRandom() returns a random element of itself.
Also have a look at this forum thread where several strategies to randomize images from different pages are discussed.

How to make PHP code to check if title contains certain word? (Wordpress Shopp)

I'm using the WordPress Shopp plugin to sell products on my WordPress site. So far I love this plugin but I'm trying to do something custom and unfortunately I'm not good enough with PHP to figure this out on my own (I've done tons of research too!).
Here is what I am trying to accomplish...
To display the product title for each individual product Shopp uses the following code to reference the product title...
<?php shopp('product','name'); ?>
What I am trying to do is reference the product name and if for example the product name says something like 'FLIR H-Series Thermal Imaging Camera' I want my code to detect whether or not the product title contains the word 'FLIR' in it and if it DOES contain the word 'FLIR' I want it to echo a FLIR logo image (let's say the URL of the image is http://example.com/images/flir.jpg). Alternatively I would want it to echo a Thermal-Eye logo image anytime the word 'Thermal-Eye' is found in the product title, etc. How could I go about modifying the code to do this?
Hopefully this make sense, thanks for any help!
<?php
$keywords = array('FLIR', 'Thermal-Eye');
foreach($keywords as $word)
{
if(preg_match('/'.$word.'/i', shopp('product','name', 'return=true')))
echo $word;
}
?>
UPDATE: If you want to echo out a unique image based on the word, rather than the word itself, you can do something like:
if(preg_match('/'.$word.'/i', shopp('product','name', 'return=true')))
echo '<img src="'.get_bloginfo('stylesheet_directory').'/images/'.$word.'.jpg" />';
So in the case of FLIR, you just need to make sure FLIR.jpg exists within the images folder in your theme directory.
Best thing to do is to play around with it and see what works for you.

Categories