如何创建自定义WordPress幻灯片插件?

时间:2012-03-24 作者:Mouse Hello

如何创建自定义wordpress幻灯片插件?我一直在谷歌上寻找教程,但我找不到,你们介意在这里提供一个教程或链接到其他自定义幻灯片插件教程吗?

2 个回复
最合适的回答,由SO网友:Ole Henrik Skogstrøm 整理而成

所以这是我的幻灯片放映实施:

这是我用来基于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,但您可以选择返回的图像大小。最后一个参数设置缩略图图像大小,如果为空,则不会返回缩略图。

希望这不是过分。:)

SO网友:David

因为你的问题有点牵强,这里有一个简单的实现WP_Query, 一个简单的to install Javascript Slider, 和Post Thumbnails.

将此代码片段添加到您想要的页面滑块

function blogSlider() {

// The Query 
$the_query = new WP_Query( \'posts_per_page=5\' );

// The Loop 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
    <?php if ( has_post_thumbnail() ) { the_post_thumbnail( \'large-thumb\' ); } ?>
    <div class="flex-caption">
      <h3 class="larger"><?php the_title(); ?></h3>

      <?php the_excerpt(); ?> // This is really dealers choice, however for this purpose it should show you a good example of how the data is being iterated through.

    </div>
  </li>
<?php endwhile;

// Reset Post Data
wp_reset_postdata();
}
将这些添加到您的函数中。php:

//Enqueue Slider Script
wp_enqueue_script( \'slider\', get_template_directory_uri() . \'/xxx/jquery.flexslider-min.js\', \'jquery\' );
//Enqueue Slider Style
wp_enqueue_style( \'slider\', get_template_directory_uri() . \'/xxx/flexslider.css\' );
以及:

//Add the Thumbnail Image size 
if ( function_exists( \'add_image_size\' ) ) { 
  add_image_size( \'large-thumb\', 960, 276, true );
}
您需要确保下载并提取Flexslider 或任何其他您喜欢的滑块脚本。

提取Flexslider后,您将使用Slider将此片段放入页眉/页面/Yepnope:

//Scripts to place
 <script type="text/javascript">
   jQuery(window).load(function() {
    jQuery(\'.flexslider\').flexslider({
      controlsContainer: "#none",
    });
 </script>
一旦准备好输出滑块,请将此代码粘贴到希望输出滑块的任何位置:

<div class="flexslider">
  <ul class="slides">
    <?php blogSlider(); ?>
  </ul>
 </div>
祝你好运,我希望这能让你走上一条足够好的道路:)

下面是添加自定义Post类型的代码段,函数可以循环使用该类型

//Generic CPT Snippet 
function 46685_slider_type() {
    register_post_type( \'slider\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Featured Images\' ),
                \'singular_name\' => __( \'Featured Image\' ),
                \'add_new\' => __( \'Add Another Featured Image\' ),
                \'add_new_item\' => __( \'Add New\' ),
                \'edit_item\' => __( \'Edit Featured Image\' ),
                \'new_item\' => __( \'New Featured Image\' ),
                \'view_item\' => __( \'View Featured Image\' ),
                \'search_items\' => __( \'Search Featured Image\' ),
                \'not_found\' => __( \'No Featured Image found\' ),
                \'not_found_in_trash\' => __( \'No Featured Images found in trash\' )
            ),
            \'public\' => true,
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\' ),
            \'capability_type\' => \'page\',
            \'rewrite\' => array("slug" => "slider"), // Permalinks format
            \'menu_position\' => 9 // int
        )
    );
}
//Add CPT to System some systems may require a rewrite flush.
add_action( \'init\', \'46685_slider_type\' );
您还需要将$the\\u查询更改为:

$the_query = new WP_Query( array(\'posts_per_page\' => \'5\', \'post_type\' => \'slider\') );

结束

相关推荐

U-plugins文件夹中的插件未加载

我正在尝试通过将插件放入mu-plugins 文件夹但是如果插件放在文件夹中,它就不会被加载。我尝试了一些流行的插件,如W3 Total Cache、Yoast的WordPress SEO,但都没有加载。WordPress是否只加载中只有单个文件的插件mu-plugins?