Skip to content

Instantly share code, notes, and snippets.

@gicolek
Last active May 28, 2024 13:56
Show Gist options
  • Save gicolek/cf37b57cfbdc64b7762187b40c96bb87 to your computer and use it in GitHub Desktop.
Save gicolek/cf37b57cfbdc64b7762187b40c96bb87 to your computer and use it in GitHub Desktop.
Custom WordPress Slider Shortcode
<?php
add_shortcode( 'wp_doin_slider', 'wp_doin_slider_func' );
/**
* @hook slider_test
*/
function wp_doin_slider_func( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'ids' => '1,2,3,4',
), $atts, 'bartag' );
// in here, we fetch the ids provided by the user and create an array out of theme to be later used
// each id corresponds to the id of an image uploaded to the media library
$image_ids = explode( ',', $atts['ids'] );
if ( !empty( $image_ids ) ):
ob_start();
?>
<div class="slider__test">
<?php
$counter = 1;
foreach ( $image_ids as $id ):
?>
<div class="slide slide--<?php echo $counter; ?> slide--active">
<?php echo wp_get_attachment_image( $id, 'medium' ); ?>
</div>
<?php
$counter++;
endforeach;
?>
</div>
<?php
endif;
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment