jQuery Backstretch and PHP - php

I am building a WordPress website where I want a fullscreen homepage background slider.
I really like the jQuery backstretch plugin here: https://github.com/srobbin/jquery-backstretch
(If you take a quick peek at how it works there, choose the second on the Demo dropdown.)
To make the plugin work you need to use this JS snippet:
<script>
$.backstretch([
"http://dl.dropbox.com/u/515046/www/outside.jpg"
, "http://dl.dropbox.com/u/515046/www/garfield-interior.jpg"
, "http://dl.dropbox.com/u/515046/www/cheers.jpg"
], {duration: 3000, fade: 750});
</script>
As you can see, the images need to be declared within the JS. I need to be able to upload as many images as I want, run a loop to get the URL's for each image then display them. I will need to pass these via PHP.
The PHP snippet for getting each image will be: <?php the_field('background_image'); ?>
How can I alter the script to run a loop and get the image/s via PHP?
Thanks in advance
Edit:
This is the loop to display all of my images using ACF plugin and the options page add-on: '
<ul>
<?php while(has_sub_field('slider_images','option')): ?>
<li><img src="<?php the_sub_field('slider_image'); ?>" /></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>'

I managed to do it with the following code:
<?php
$images = get_field('bg_images','option');
if ($images):
foreach($images as $image): ?>
<?php $imageList .= '"'. $image['url'] .'",'; ?>
<?php endforeach;
endif;
$imageList = rtrim($imageList, ',');
?>
<script>
$.backstretch([
<?php echo $imageList; ?>
], {duration: 3000, fade: 750});
</script>
Thanks for the help #mcNab

It depends on how these images are associated with posts/options, that is not clear from your question. The PHP you've suggested there looks like an option attached to an individual page rather than array full of images. However for the sake of your example let's assume that you want to pull the background image out of the first 5 posts in a custom post type called home_slider and use those;
<?php
$imgList = '';
$homeslider_query = new WP_Query(array('post_type' => 'home_slider', 'numberposts' => '5', 'orderby' => 'menu_order', 'order' => 'ASC' ));
if ( $homeslider_query->have_posts() ) :
while ($homeslider_query->have_posts()) : $homeslider_query->the_post();
// Build your list of images
$imgList .= '"'. the_field('background_image') .'",';
endwhile;
endif;
// Trim trailing comma from list
$imgList = rtrim($imgList, ',');
?>
Now, as you have the script tags there that tells me the JS is in the page itself, probably the head, rather than a .js file so;
<script>
$.backstretch([
<?php echo $imgList; ?>
], {duration: 3000, fade: 750});
</script>

Related

Replacing Image Source Value With a PHP Variable Based on Device Width

I'd like to be able to able to swap an img's src value with a variable created in PHP.
I currently am using javascript to determine the device width. This is happening within the .php file containing the Wordpress loop. Upon recognizing the device width, I would like to change the img's src value accordingly.
I can successfully retrieve the PHP variables using this JS function however only when the function is written in the loop, and as you all know, this will duplicate the function for each post and is resulting in an error.
I need to be able to calculate these given PHP variables outside the loop and then inject them into the img src value inside the loop.
I recognize I may have more errors in this code than what I am looking to resolve! I have been working on this for some time and this particular issue has become quite troubling. Thanks in advance.
Current Code in Use:
<?php query_posts( array ( 'home' => 'WordPress Themes', 'orderby' => 'rand', 'posts_per_page' => -1 ) ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<script>
function getImagePath(){
if (window.matchMedia('(max-device-width: 1920px)').matches) {
var theSizedImage = <?php the_field('desktop_image'); ?>
}if (window.matchMedia('(max-device-width: 1280px)').matches) {
var theSizedImage = <?php the_field('tablet_image'); ?>
}if (window.matchMedia('(max-device-width: 600px)').matches) {
var theSizedImage = <?php the_field('mobile_image'); ?>
}
return theSizedImage;
}
</script>
<img src="pixel.gif" onload="this.onload=null; this.src=getImagePath();" />
<?php endwhile;
wp_reset_query();
?>
So after much further tinkering, I was able to come up with a solution.
This does in fact work, however I wonder if there is a far more elegant solution.
You will note my query has changed slightly as I improved the page and took care of errors. The main problem with the above code was that the variables for the image URLs were getting defined only once. The solution I have provided below will rerun for each of the posts in the loop.
<?php query_posts( array ( 'category__not_in' => array( 1, 4, 5, 6, 7 ), 'orderby' => 'rand', 'posts_per_page' => -1 ) ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<script>
if (screen.width <= 1920) {document.write("<img src='<?php the_field('desktop_image');?>' />");}
if (screen.width <= 1280) {document.write("<img src='<?php the_field('tablet_image');?>' />");}
if (screen.width <= 600) {document.write("<img src='<?php the_field('mobile_image');?>' />");}
if (screen.width > 1920){document.write("<img src='<?php the_field('full_resolution');?>' />");}
</script>
<?php endwhile;
wp_reset_query();
?>

Wordpress PHP within PHP

I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.
Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/
You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>

wordpress - how to get jquery slide caption and title

I am beginner in PHP, so maybe for some of you this question is ridiculous.
I would like to get image title and caption from jquery slider. (Themeforest Delight theme - http://www.pixedelic.com/themes/delight/), to show them in the same way as in (http://themes.themegoods.com/dk_wp/).
I have tried almost everything.
Thanks in advance for your help.
Here is the code from header:
<?php
function default_bg(){
if(get_pix_option('pix_general_background')=='slideshow'){ ?>
<div class="pix_slide" data-position="fixed" data-top="0" data-bottom="not">
<?php
$slide_general = get_pix_option('pix_array_slide_general_');
foreach ($slide_general as $slide) {
echo '<div data-src="'. $slide .'" data-thumb="'. get_pix_thumb($slide, 'exTh') .'" data-content="'. get_pix_content($slide) .'"></div>';
}
?>
I have found some script, which get the content, but can't implement it.
Here it is:
content: jQuery('#pix_credits_pictures').html(),
events: {
show: function(event, api) {
api.set('content.text', jQuery('#pix_credits_pictures').html());
}
},
You will need to modify the way the jQuery slider to include custom attributes for the images to include the title and the caption. Something like:
<?php
$slide_captions = array("caption1","caption2"...);
$slide_title = array("title1","title2"...);
?>
<?php
function default_bg(){
if(get_pix_option('pix_general_background')=='slideshow'){ ?>
<div class="pix_slide" data-position="fixed" data-top="0" data-bottom="not">
<?php
$slide_general = get_pix_option('pix_array_slide_general_');
$c = 0;
foreach ($slide_general as $slide) {
echo '<div data-src="'. $slide .'" data-thumb="'. get_pix_thumb($slide, 'exTh') .'" data-content="'. get_pix_content($slide) .'" image_title="'.$slide_title [$c].'" image_caption="'.$slide_captions[$c].'" class="slide_item"></div>';
$c++;
}
?>
You can implement a more elegant solution for "storing" the titles and captions instead of an array, I guess that depends on the way your plugin works and how it is storing the images.
Next up you can access the title and captions using jQuery like so:
var image_title= $('.slide_item').attr('slide_title');
var image_caption= $('.slide_item').attr('image_caption');
Obviously you will have to decide where exactly you want to use the title and when, but that is how you access them. The best way is probably to modify the slider library on the active image event.

Drupal 7: Display images instead of nodes titles

Normaly in Drupal 7 we have node.tpl.php:
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
It is taking $node_url and putting it on a Title in every Node.
I do have 5 nodes (pages) displayed:
First
Second
Third etc
I have created images First.gif, Second.gif and I want to load that images instead of Title.
I did check various implementation, but didn't find any resolution for me.
[Update] I did try to edit template.php file and to add functions for replacing title with image-if images exists. I need this in Drupal 7 - please see here - http://drupal.org/node/221854
Is there any help? Thanks..
Since each node has it's own id then what I'd suggest to you, is to have in your specific files folder images such as node-12, node-13 etc.
In your node.tpl.php
<?php
// path to our file depending on node id
$file = "path/to/file/node-{$node->nid}.gif";
// check if file exists
if (file_exists($file)) {
// theme $title as image with theme_image()
$title = theme('image', array('path' => $file));
}
?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
However I'm sure this is not the best way of development but it work for your question.

Multiple loop working, function inside isn't

I'm using this piece of code I found (http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage) to display the first image uploaded to a post on the homepage next to the excerpts of posts. I'm doing this outside the main loop on the homepage, and have been having problems. When I do rewind_posts() to get the same loop results, it works fine, but when I try to create a different loop, this code snippet breaks down:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images)
{
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";
}
I have tried the methods in The_Loop#Multiple_Loops_in_Action in the docs, and they work, meaning I can get normal output after the loop, but my snippet above doesn't work. Any idea if there is a conflicting method call or something that's going on that's stopping it from working? Would appreciate some help, thanks!
To be more specific:
<?php $my_query = new WP_Query('category_name=Daily Photo&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<p>a</p>
<?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images) {
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";} ?>
<?php endwhile; ?>
will output <p>a</p>, but not the <li><img /></li> code I need in the snippet. Whereas if I use rewind_posts();, everything works, and I get the <li><img /></li> code, but I don't want to use the same loop that I had been using previously. I'm using this to display a daily photo in the sidebar, that pulls from the "Daily Photo" category. I will exclude Daily Photos from the main loop, and only want to use them to draw images from in that snippet.
Try removing the if statement for starters. Remove any references to images and see if it outputs your html without the image source. If that's the case then images isn't getting properly assigned and it's never executing the code.
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
This code checks to see if there is an
image uploaded to the gallery.
Now you need to troubleshoot the above statement and see why you're not getting any images.
Also, it looks like there is a plugin to do just this if you don't want to continue messing with the code.
Another thing to keep in mind is that this code is pretty old and may not be compatible with your version of wordpress.

Categories