Flexslider Wordpress - 为特色图像添加字幕和外部链接

时间:2012-03-07 21:37:28

标签: wordpress custom-post-type caption flexslider

所以我一直在尝试将字幕和链接集成到Nettuts上的Envato FlexSlider插件中。

http://wp.tutsplus.com/tutorials/create-a-responsive-slider-plugin-with-flexslider/

Adding caption area to 'Flexslider Plug-in' 不起作用。

这是envato-flexslider.php文件,其中发生了所有魔法。

 <?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();
 }


 ?>

至于添加外部链接到特色图像,我试图通过slider-img-type.php文件在我的自定义帖子类型中设置自定义字段,但是没有用。

感谢您的帮助, 达斯汀

2 个答案:

答案 0 :(得分:2)

好的,这就是我能够将链接和标题集成到FlexSlider中的方式。希望这能帮助任何一直像我一样挣扎的人。在envato-flexslider.php中,这是函数efs_get_slider()所需的内容。只需确保分别在幻灯片image_caption和image_link中命名自定义字段。

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;
}

答案 1 :(得分:1)

所以基本上你可以在这里添加任何你想要的东西:

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;

因此,假设您想添加一些自定义字段信息,那么您只需将变量设置为该信息,然后将其添加到滑块变量中。

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;

但是,所有这一切都变得混乱。让我们简化一下,然后做一个正确的sprintf并将其包装成几行。

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;