The following code would provide a very basic interface. The idea is the following. On the edit page you get another meta box "Slider", where you can select which attached media shall be used for the slider. Here of course could be more UX work done. Right now, it only shows the already attached media. So the editor needs to add the media, save the post and can then select. There could be done some improvements.
Once, the editor defined some attached media to be for the slider, you won't use get_attached_media( 'image' )
anymore, but get_attached_slider()
. Basically this function also outputs Post Objects, but only of the attached media, which shall be displayed in the slider.
Hope, this gives you a start for what you need.
<?php
add_action( 'add_meta_boxes', 'slider_metabox' );
function slider_metabox(){
add_meta_box(
'slider-metabox',
__( 'Slider Pictures', 'sl' ),
'slider_metabox_render',
'post' //Use 'post' to display on Posts, 'page' to display on Pages
);
}
/**
* This function renders the Metabox
**/
function slider_metabox_render( $post ){
$slider_attachments = get_post_meta( $post->ID, 'slider_attachments', true );
$all_attachments = get_attached_media( 'image', $post->ID );
if( empty( $slider_attachments ) )
$slider_attachments = array();
/*
* We get our attached media and the media, which is already supposed
* to be in the slider. We loop through all the attached media
* output a checkbox and if the single media is
* already for the slider, we check the checkbox
**/
?>
<ul>
<?php foreach( $all_attachments as $s ): ?>
<li data-attachment="<?php echo $s->ID; ?>">
<label for="attachment-<?php echo $s->ID; ?>">
<?php echo wp_get_attachment_image( $s->ID, 'post-thumbnail' ); ?>
</label>
<input id="attachment-<?php echo $s->ID; ?>" type="checkbox" <?php
if( in_array( $s->ID, $slider_attachments ) ):
echo 'checked="checked" ';
endif;
?>name="slider_attachments[]" value="<?php echo $s->ID; ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php
}
/**
* Lets save our slider media
**/
add_action( 'save_post', 'slider_attachment_save' );
function slider_attachment_save( $post_id ){
if ( wp_is_post_revision( $post_id ) )
return;
if( isset( $_POST['slider_attachments'] ) )
update_post_meta(
$post_id,
'slider_attachments',
$_POST['slider_attachments']
);
}
/**
* get_attached_slider
* Use this instead of get_attached_media() to retrieve only the slider images
**/
function get_attached_slider( $post_id = null ){
if( $post_id == null )
$post_id = get_the_ID();
if( !is_numeric( $post_id ) ){
$error = new WP_Error();
$error->add( 'post-id', 'No valid post ID' );
return $error;
}
$attachment_ids = get_post_meta( $post_id, 'slider_attachments', true );
if( empty( $attachment_ids ) || ! is_array( $attachment_ids ) )
return false;
$args = array(
'post_type' => 'attachment',
'post__in' => $attachment_ids,
'post_status' => 'any',
'posts_per_page' => count( $attachment_ids )
);
$query = new WP_Query( $args );
$attachments = $query->posts;
return $attachments;
}
?>
More information on how to realize this: