Flexslider Wordpress - Adding Captions and External Links to Featured Images
-
09-12-2019 - |
Question
So I have been attempting to integrate captions and links into the Envato FlexSlider plugin found over at Nettuts.
http://wp.tutsplus.com/tutorials/create-a-responsive-slider-plugin-with-flexslider/
Adding caption area to 'Flexslider Plug-in' does not work.
Here is the envato-flexslider.php file where all the magic happens.
<?php
/*
Plugin Name: Envato FlexSlider
Plugin URI:
Description: A simple plugin that integrates FlexSlider
(http://flex.madebymufffin.com/) with WordPress using custom post types!
Author: Joe Casabona
Version: 0.5
Author URI: http://www.casabona.org
*/
/*Some Set-up*/
define('EFS_PATH', WP_PLUGIN_URL . '/' . plugin_basename( dirname(__FILE__) ) . '/' );
define('EFS_NAME', "Envato FlexSlider");
define ("EFS_VERSION", "0.5");
/*Files to Include*/
require_once('slider-img-type.php');
/*Add the Javascript/CSS Files!*/
wp_enqueue_script('flexslider', EFS_PATH.'jquery.flexslider-min.js', array('jquery'));
wp_enqueue_style('flexslider_css', EFS_PATH.'flexslider.css');
/*Add the Hooks to place the javascript in the header*/
function efs_script(){
print '<script type="text/javascript" charset="utf-8">
jQuery(window).load(function() {
jQuery(\'.flexslider\').flexslider({
animation: "slide",
slideshowSpeed: 6000,
animationDuration: 300,
directionNav: false,
controlNav: false
});
});
</script>';
}
add_action('wp_head', 'efs_script');
function efs_get_slider(){
$slider= '<div class="flexslider">
<ul class="slides">';
$efs_query= "post_type=slider-image";
query_posts($efs_query);
if (have_posts()) : while (have_posts()) : the_post();
$img= get_the_post_thumbnail( $post->ID, 'large' );
$slider.='<li>'.$img.'</li>';
endwhile; endif; wp_reset_query();
$slider.= '</ul>
</div>';
return $slider;
}
/**add the shortcode for the slider- for use in editor**/
function efs_insert_slider($atts, $content=null){
$slider= efs_get_slider();
return $slider;
}
add_shortcode('ef_slider', 'efs_insert_slider');
/**add template tag- for use in themes**/
function efs_slider(){
print efs_get_slider();
}
?>
As for adding external links to the featured images, i have tried to setup custom fields in my custom post types through the slider-img-type.php file and that has not worked.
Thanks for your help, Dustin
Solution
Ok, so this is how I was able to integrate both links and captions into FlexSlider. Hope this helps anyone who has been struggling as mightily as I have. In envato-flexslider.php here is what you need for function efs_get_slider(). Just make sure you name your custom fields in your slides image_caption and image_link respectively.
function efs_get_slider(){
$slider= '<div class="flexslider">
<ul class="slides">';
$efs_query= "post_type=slider-image";
$myposts = get_posts($efs_query);
foreach($myposts as $post) : setup_postdata($post);
$img= get_the_post_thumbnail( $post->ID, 'full' );
$slide_link= get_post_meta( $post->ID, 'image_link', true);
$slide_caption= get_post_meta( $post->ID, 'image_caption', true);
$slider.='<li><a href='.$slide_link.'>'.$img.'</a><p class="flex-caption">'.$slide_caption.'</p></li>';
endforeach;
$slider.= '</ul>
</div>';
return $slider;
}
OTHER TIPS
So basically you can add in anything you want right here:
if (have_posts()) : while (have_posts()) : the_post();
$img= get_the_post_thumbnail( $post->ID, 'large' );
$slider.='<li>'.$img.'</li>';
endwhile; endif; wp_reset_query();
$slider.= '</ul>
</div>';
return $slider;
So let's say you wanted to add some custom field information, then you just have to set a variable as that info and then add it tot the slider variable.
if (have_posts()) : while (have_posts()) : the_post();
$img= get_the_post_thumbnail( $post->ID, 'large' );
$caption= get_post_meta($post->ID, 'custom_field', true); //get custom field
$slider.='<li>';
$slider.= $img;
$slider.= '<span>'.$caption.'</span>';
$slider.='</li>';
endwhile; endif; wp_reset_query();
$slider.= '</ul>
</div>';
return $slider;
But still, all this is getting cluttered. Let's simplify this and just do a proper sprintf and wrap this into a few lines.
if (have_posts()) : while (have_posts()) : the_post();
$slider .= sprintf('<li>%1$s<span>%2$s</span></li>',
get_the_post_thumbnail( $post->ID, 'large' ),
get_post_meta($post->ID, 'custom_field', true)
);
endwhile; endif; wp_reset_query();
$slider.= '</ul>
</div>';
return $slider;