Skip to content

Instantly share code, notes, and snippets.

@mrron313
Created September 12, 2017 09:43
Show Gist options
  • Save mrron313/9115655942877386a98e25a07389476d to your computer and use it in GitHub Desktop.
Save mrron313/9115655942877386a98e25a07389476d to your computer and use it in GitHub Desktop.
This is slider shortcode for wordpress with custom post registration
/**** Register Custom Post *****/
function industry_theme_custom_post() {
register_post_type( 'industry-slides',
array(
'labels' => array(
'name' => __( 'Slides' ),
'singular_name' => __( 'Slide' )
),
'supports' => array('title', 'editor', 'custom-fields', 'thumbnail', 'page-attributes'),
'public' => false,
'show_ui' => true
)
);
}
add_action( 'init', 'industry_theme_custom_post' );
function industry_slides_shortcode( $atts) {
extract( shortcode_atts( array(
'post_count_by_user' => '2',
'post_type_by_user' => 'industry-slides',
), $atts));
$args = array(
'post_type' => $post_type_by_user ,
'posts_per_page' => $post_count_by_user
);
$query = new WP_Query( $args );
$random_industry_slide = rand(20,30);
$return_string = '
<script>
jQuery( window ).load(function($) {
jQuery("#idustry-slider-'.$random_industry_slide.'").owlCarousel({
items : 1 ,
loop : true ,
nav : true ,
dots : true ,
autoplay : false ,
navText : ["<i class=\'fa fa-angle-left\'></i>" , "<i class=\'fa fa-angle-right\'></i>"]
});
});
</script>
<div id="idustry-slider-'.$random_industry_slide.'" class="owl-carousel slides-div">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
$return_string .= '
<div style="background-image:url('.get_the_post_thumbnail_url($post_id,'large').')" class="single-slider">
<div class="single-slider-inner">
<div class="container">
<div class="row">
<div class="col-md-6">
<h2>'.get_the_title($post_id).'</h2>
'.wpautop(get_the_content($post_id)).'
</div>
</div>
</div>
</div>
</div>
' ;
}
}
$return_string .= '</div>' ;
wp_reset_query();
return $return_string ;
}
add_shortcode( 'industry-slides', 'industry_slides_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment