I have created a repeater field called: "random_images".
It contains the subfields: "random photo one" (to five)
One random image at the time from these five should be displayed on my front-end. The images should change accordingly: When the page loads, one random image should be displayed. After a couple of seconds, the image should change to another.
How do I do it?
I am new to ACF and PHP. This is what I have come up with so far:
if (get_row_layout() == 'random_images') {
$rows = get_sub_field('random_images');
$rand_row_image = array_rand($random_images, 1);
$image = wp_get_attachment_image_src($rand_image, 'full');
}
I'm going to assume you have a repeater field called random_images that has a repeatable subfield (image field) called image, so, when you go to your post editor (or wherever you place the repeatable field), you can repeat the image field as many times as you want and put as many images as you want there.
Then, if you put your code in single.php (or you template parts or other template files), you can do this:
<?php
// ... template code already there
$random_images = get_field('random_images');
//The line below will mix the array randomly, so you always pick a random element from it
shuffle($random_images);
$random_img_url = $random_imges[0]['image']['url'];
?>
Note: How to get the URL might change depending on how you configured your fields when you created them, but that is the idea.
Note: If you want to use get_field() outside the loop, you are going to have to use the post id in this way get_field('random_images', $post_id);
The code above will give you one of the images you placed in the repeater field, so now you can render that image in you page or post, and the image will be different in every page load.
<img src="<?php echo $random_img_url; ?>" />
To this point, you used PHP to render a random image but with PHP you can't change the image after the page is loaded, so you will have to use jQuery for that (you can use vanilla JavaScript but to make things easy I will explain this with jQuery).
If you don't have a custom file to put JS code in the theme you are developing, then you will have to create a file called custom.js or whatever name you want to give it, and then enqueue that file with wp_enqueue_script. I will not explain more of this because it is not directly related to the question.
Now you have your custom.js file, but you will need a way to get the other image URLs from the page with your random images, so, to achieve that, you can do the following just where we left the php code above.
<?php
$rand_imgs_urls = [];
foreach($random_images as $image) {
$rand_imgs_urls[] = $image['image']['url'];
}
// Now, instead of the <img /> tag we used above, we will use this
?>
<div class="random-img" data-images="<?php echo json_encode($rand_imgs_urls); ?>">
<img src="<?php echo $rand_imgs_urls[0]; ?>" />
</div>
Once you rendered the image, now you can use jQuery to get the images placed in the data-images attribute of the div above, and change the image src every few seconds. So, in the custom.js file you can put something like this:
jQuery(document).ready(function($) {
var images = JSON.parse($('.random-img').data('images'));
var randomImage = images[Math.floor(Math.random()*images.length)];
setInterval(function(){ $('.random-img img').attr('src', randomImage); }, 5000);
});
With that you will have the image changing every 5 seconds.
Related
I have looked through a couple of threads, with no luck to this problem. I'm just trying to have different images appear on a new page load for the homepage only. The CSS element I'm trying to change is
.header_container
The html tag is this:
<header id="masthead" class="site-header header_container" role="banner">
I have three images that I would like to rotate in. I have tried to call a php file that calls the images from /images/ and that didn't work.
Here is the site ---> http://www.l4sb.com
Thanks for any help you might have.
Why not store available image paths in PHP array like:
$aBgImages = array('img01.jpg','img02.jpg');
Then add a a <script> tag with something like:
var myElem = document.getElementById['header'];
myElem.style.backgroundImage="url('<?php echo array_rand($aBgImages) ?>')";
The above is not tested, just a rough example.
I have a image gallery with 15 pics, it shows the first image in normal size and the rest are thumbs on the bottom, when you click the thumbs it replaces the normal size image with that one, and if you click the normal sized image it shows on a lightbox style popup with the image description.
This gallery layout is called Galleria and its part of the SIG Pro joomla plugin:
http://demo.joomlaworks.net/simple-image-gallery-pro
The image descriptions for each image are shown on the popup, but I would like to remove this behavior and show the description below the normal sized image.
My question is regarding PHP, I'm very noob at this.
For what I figured out I can print all the thumbs descriptions with this:
<?php foreach($gallery as $count=>$photo): ?>
<p class="sigProGalleriaTargetTxt"><?php echo $photo->captionDescription; ?></p>
<?php endforeach; ?>
But what I want to print is only the description of the image that is selected.
I tried this:
<p class="sigProGalleriaTargetTxt"><?php echo $gallery[0]->captionDescription; ?></p>
But this only prints the first image description, because it has the value 0 (first image).
I see that I need to increment the value of $gallery according to the selected image.
How can I achieve this?
Here is the complete php file:
http://codepad.org/MlPbgPzl
Thank you,
What about using som javascript/dom to set the captions? its a bit of a hacky but, i would try to have an onClick Event which calls a function to apply the description.
in the thumbs sections something like:
<span class="sigProLinkWrapper">
<a onClick="setDesc('<?php echo $photo->captionDescription; ?>');" href="...
and for the description:
// leaving the <?php tag like this, so you get always the first description
// but adding an id tag
<p id='photoDescription' class="sigProGalleriaTargetTxt">$gallery[0]->captionDescription; ?></p>
and finally replacing it with some javascript (or jquery if youre using that)
function setDesc(photoDesc)
{
var descContainer = document.getElementById("photoDescription").innerHTML= photoDesc;
}
maybe it helps :)
new to Drupal 6. I see a lot of slideshow modules out there, but it seems like most of them require quite a lot of configuration, and would be cumbersome for the user to change later. Some of my pages have sliders, and others have single images in the same spot. I don't want to have a separate node for each slider. The client can't handle that. I would like to have the images uploaded via the attachment section in the page itself, and fields for the image title and alt text. Then I would want to take that and display the image along with the title and alt text as a slideshow, with buttons to switch to previous and next. Is it possible to do this?
The jquery slideshow module almost does what you want, but it doesn't have the next/previous buttons.
I'd recommend just finding a javascript slideshow product that you want to use, and then manually integrate it into a node template.
Recently, I integrated jqFancyTransitions into a Drupal 6 project. The content type contains a image field, set to allow multiple values.
I installed the library in my theme, then, in the content type specific node template, I added this:
mycontenttype-node.tpl
<script src="/sites/all/themes/mytheme/jqFancyTransitions.1.8.min.js" type="text/javascript"></script>
<div id='ft'>
<?php foreach ($node->field_my_photo_field as $image): ?>
<img src="/sites/default/files/imagecache/my_photo_imagecache_preset/<?php print base_path() . str_replace(' ', '%20', $image['filepath']); ?>" alt="" />
<?php endforeach; ?>
</div>
<script>
$('#ft').jqFancyTransitions({ width: 350, height: 300, navigation: true, effect: 'wave' });
</script>
Note, I'm using an imagecache preset to crop & scale the images to 350x300, and I had to str_preplace spaces because jqfancytransitions didn't like them. That's only because it was an easier solution than either requiring users not to use spaces or modifying their file names.
I have an interesting question, or atleast I think it is.
I have this website http://21-card-games.com/best/casino-reviews/casino-casino-titan.asp. You will see the image and below it the content.
Is it possible to combine the image with the text and left align the image and make the text flow next to the image.
The pages are all dynamically created, The content on the page is a variable that I import from a mysql db...
Here is some of the coding:
<div id="yui-main"><div class="yui-b"><?php echo $g_page_content2 ?><br />
<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/standard_images_screenshot_1.php');?><?php echo $g_content_text1 ?>
<?php /* This is the second banner for this page */ include($_SERVER['DOCUMENT_ROOT'].'/includes/banners/'.$row_rs_settings['g_sites_affiliate_ads2'].'.php');?></div>
The $g_content_text1 is the variable that pulls in the text, and the '/includes/standard_images_screenshot_1.php' is the image. The include image file is part of a script that creates the image on the fly (imagick).
However this is more or less what I where thinking:
to create a regex that replaces the first occurance of <p> with this include($_SERVER['DOCUMENT_ROOT'].'/includes/banners/'.$row_rs_settings['g_sites_affiliate_ads2'].'.php' but I am not sure if the php will still execute if I do that...
Any ideas on how I can accomplish this would be appreciated.
I don't get it.
Why you want to create a regexp to let the text flow on the left of the image?
This is a css issue.
Just add float:left to the image.
Basically I have a slightly non-standard implementation of FancyBox. By default you have to include a link to the large version of the image so that the Lightbox can display it. However, in my implementation, the image link URLs point to a script rather than directly to the image file. So for example, instead of:
<a href="mysite/images/myimage.jpg" rel="gallery">
I have:
<a href="mysite/photos/view/abc123" rel="gallery">
The above URL points to a function:
public function actionPhotos($view)
{
$photo=Photo::model()->find('name=:name', array(':name'=>$view));
if(!empty($photo))
{
$this->renderPartial('_photo', array('photo'=>$photo, true));
}
}
The "$this->renderPartial()" bit simply calls a layout file which includes a standard HTML tag to output.
Now when the user clicks on a thumbnail, the above function is called and the large image is displayed in the Lightbox.
Now if the user right clicks on the thumbnail and selects "open in new tab/window" then the image is displayed in the browser as per normal, i.e. just the image. I want to change this so that it displays the image within a layout.
In the above code I can include the following and put it in an IF statement:
$this->render('photos', array('photo'=>$photo));
This will call the layout file "photos" which contains the layout to display the image in.
I have a specific limitation for this - the image URL must remain the same, i.e. no additional GET variables in the URL. However if we can pass in a GET variable in the background then that is OK.
I will most likely need to change my function above so that it calls a different file for this functionality.
EDIT: To demonstrate exactly what I am trying to do, check out the following:
http://www.starnow.co.uk/KimberleyMarren
Go to the photos tab and hover over a thumbnail - note the URL. Click the thumbnail and it will open up in the Lightbox. Next right click on that same thumbnail and select "open in new tab/new window". You will notice that the image is now displayed in a layout. So that same URL is used for displaying the image in the Lightbox and on its own page.
The way StarNow have done this is using some crazy long JavaScript functionality, which I'm not too keen on replicating.
The html link should point to the layout showing the image on a new page by default, e.g.:
<a href="mysite/images/show/123" rel="gallery">
Before the lightbox opens, append a query string to the url in order to distinguish it from the normal link and load the layout for the lightbox. As soon as the image is loaded in the lightbox, change the link back to its original state.
$("a[rel=gallery]").fancybox({
'onStart': function (selectedArray, selectedIndex, selectedOpts) {
var el = $(selectedArray[selectedIndex]);
el.attr('href', el.attr('href') + '?mode=lightbox');
},
'onComplete': function (currentArray, currentIndex, currentOpts) {
var el = $(currentArray[currentIndex]);
el.attr('href', el.attr('href').split("?")[0]);
}
});
You will then have to process the following link in order to return the lightbox layout:
<a href="mysite/images/show/123?mode=lightbox" rel="gallery">
You should be able to modify the JavaScript function that generates the HTML with the <img /> tag to link the image to such a page. Although, if you are trying to make it so that selecting "Open image in new tab" opens a page like this, then that might be impossible (unless there is some sort of crazy cookie/session implementation to alternate between the image script just passing an image and generating a page, which I think could be possible). To assign a new href for the link to have when you click "Open link in new tab" should be quite possible by just modifying the JavaScript function.
Could you clarify what exactly you are attempting to do? Open link in new tab or open image in new tab?
Edit: It appears that the FancyBox script is changing the href of your link to point directly to the image. You would need to find where in the script it is selecting each link tag with rel="gallery" and replacing the href to point to the images; you will want it to not change the href if you want it left as "mysite/photos/view/abc123", for example.
If you need the same functionality the demo site you posted is using, then this is easy to achieve, but keep in mind that the site is NOT using the same URL for both the pop-up and the standalone image page.
Click on any thumbnail with Firebug console is open, you'll notice that it's making an Ajax request to get the image from a different URL! which is an obvious behavior.
http://www.starnow.co.uk/profile/PhotosTrackView.aspx?photo_id=2129864
While the link is pointing to:
http://www.starnow.co.uk/KimberleyMarren/photos/2129864/
you see your links should point to the correct image page, in case of JS disabled browsing or right clicking (as you mentioned) AND using JS to override the link default behavior (which is redirecting you to the image page).
So for example you can have a method that will generate your image layout/page, and this should be used as href; and override the click event of the link to call a similar method (using ajax) but this time it'll retrieve the image itself to use it in your lightbox.