所以这是我的幻灯片放映实施:
这是我用来基于flexslider JQuery插件创建幻灯片的代码(谢谢David!)。它还为用户提供了一个复选框,以便用户可以选择要在幻灯片中包括哪些图片,以及要跳过哪些图片。如果您想要一个与实现David给出的解决方案所需的任何帖子无关的常规幻灯片,则这些图像是从帖子附件中拍摄的:)
这是我用来在媒体上传器中添加复选框的函数/过滤器,让客户端选择是否要在幻灯片中包含图片。
// Adding a custom checkbox to the Media Uploader
function monster_image_attachment_slideshow($form_fields, $post) {
$foo = (bool)get_post_meta($post->ID, "rt-image-link", true);
$form_fields["rt-image-link"] = array(
"label" => __("Include in gallery?"),
\'input\' => \'html\',
\'html\' => \'<label for="attachments-\'.$post->ID.\'-foo"> \' . \'<input type="checkbox" id="attachments-\'.$post->ID.\'-foo" name="attachments[\'.$post->ID.\'][rt-image-link]" value="1"\'.($foo == true ? \' checked="checked"\' : \'\').\' /> Yes</label> \',
"value" => $foo,
"helps" => __("Check this if you want to include this image in your gallery"),
);
return $form_fields;
}
// now attach our function to the hook
add_filter("attachment_fields_to_edit", "monster_image_attachment_slideshow", null, 2);
// Saving the media field
function monster_image_attachment_slideshow_to_save($post, $attachment) {
// $attachment part of the form $_POST ($_POST[attachments][postID])
update_post_meta($post[\'ID\'], \'rt-image-link\', $attachment[\'rt-image-link\']);
return $post;
}
// now attach our function to the hook.
add_filter("attachment_fields_to_save", "monster_image_attachment_slideshow_to_save", null , 2);
然后,我也在我的函数文件中使用此函数(如果您愿意,这两个函数都可以包含在插件中):
// This is the funcion that rolls out my slideshow html
function m_slideshow_outputter( $post_id ,$size = \'large\', $thumbnail_size = \'\') {
global $post;
$got_slides = false;
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'post_status\' => null,
\'post_parent\' => $post_id,
\'order\' => \'ASC\',
\'orderby\' => \'menu_order\'
);
// Getting attachmentes (images)
$attachments = get_posts( $args );
if ( $attachments ) {
$cm_output = \'<div class="flex-container"><div class="flexslider"><ul class="slides">\';
// looping through each attachment for the given post
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, $size );
$cm_src = $image_attributes[0];
$cm_width = $image_attributes[1];
$cm_height = $image_attributes[2];
// This part gets custom metainformation about the attachement.
// I have made a custom checkbox inside the media uploader. If the checkbox is checked the image is included. See the checkbox code above :)
$gallery_check_array = get_post_meta($attachment->ID, "rt-image-link");
$gallery_check = $gallery_check_array[0];
// So just include the picture if the checkbox in checked :)
if ( $gallery_check == 1) {
$got_slides = true;
$cm_output .= \'<li><img class="picture-\'.$post_id.\'-\'.$attachment->ID .\'" src="\'.$cm_src.\'" alt="Slideshow" width="\'.$cm_width.\'" height="\'.$cm_height.\'"/></li>\';
}
}
$cm_output .= \'</ul></div></div><!-- End flexslider -->\';
// Outputting the thumbnails if a thumbnail size has been entered. Ignore this or delete this part if you dont need thumbnails
if ($thumbnail_size) {
$cm_output .= \'<div class="slideshow-thumbails"><ul>\';
foreach ( $attachments as $attachment ) {
$thumbnail_attributes = wp_get_attachment_image_src( $attachment->ID, $thumbnail_size );
$cm_tumb_src = $thumbnail_attributes[0];
$cm_tumb_width = $thumbnail_attributes[1];
$cm_tumb_height = $thumbnail_attributes[2];
$thumbnail_check_array = get_post_meta($attachment->ID, "rt-image-link");
$thumbnail_check = $thumbnail_check_array[0];
// doing the same check as above for each thumbnail.
if ( $thumbnail_check == 1) {
$cm_output .= \'<li><img class="picture-\'.$post_id.\'-\'.$attachment->ID .\'" src="\'.$cm_tumb_src.\'" alt="Slideshow" width="\'.$cm_tumb_width.\'" height="\'.$cm_tumb_height.\'" /></li>\';
}
}
$cm_output .= \'</ul></div>\';
}
//If there are images in the slidehow return them, else return nothing.
if ($got_slides) {
return($cm_output);
} else {
return("");
}
// if the post has no images return nothing.
} else {
return("");
}
}
然后需要包括jquery:
// How to add custom js to Front end
add_action(\'wp_print_scripts\', \'monster_front_js\');
function monster_front_js(){
wp_register_script(\'flexslider\', get_template_directory_uri() . \'/js/jquery.flexslider-min.js\', array(\'jquery\'), \'1.0\' );
wp_register_script(\'monster_front\', get_template_directory_uri() . \'/js/monster_front.js\', array(\'jquery\', \'flexslider\'), \'1.0\' );
wp_enqueue_script(\'monster_front\');
wp_enqueue_script(\'jquery.flexslider-min.js\');
}
这是在自定义jquery中调整幻灯片:
jQuery(window).load(function() {
jQuery(\'.flexslider\').flexslider({
controlsContainer: ".flex-container",
controlNav: false,
directionNav: true
});
});
以及与幻灯片相关的css:
add_action(\'wp_print_styles\', \'monster_front_styles\');
function monster_front_styles() {
wp_register_style( \'flexslider_style\', get_template_directory_uri() . \'/css/flexslider.css\');
wp_enqueue_style(\'flexslider_style\');
}
最后!幻灯片代码已准备好插入主题前端。:)
<?php echo m_slideshow_outputter( $post->ID ,\'large\', \'thumbnail\'); ?><!-- This is the slideshow :) -->
唯一需要的参数是post id。第二个参数默认设置为large,但您可以选择返回的图像大小。最后一个参数设置缩略图图像大小,如果为空,则不会返回缩略图。
希望这不是过分。:)