Last active
May 28, 2024 13:56
-
-
Save gicolek/cf37b57cfbdc64b7762187b40c96bb87 to your computer and use it in GitHub Desktop.
Custom WordPress Slider Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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