Skip to content

Instantly share code, notes, and snippets.

@James-Roberts
Last active September 19, 2015 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save James-Roberts/ee843ed44abffdbf29ba to your computer and use it in GitHub Desktop.
Save James-Roberts/ee843ed44abffdbf29ba to your computer and use it in GitHub Desktop.
Create a portfolio carousel in Genesis.on any post or page https://llamapress.com/portfolio-carousel-genesis/
<?php
/**
* Portfolio carousel shortcode
* Author: James Roberts
*
*/
//* Enqueue Owl and font awesome
add_action( 'wp_enqueue_scripts', 'enqueStuff' );
//* Add Archive Settings option to Portolio CPT
add_post_type_support( 'portfolio', 'genesis-cpt-archives-settings' );
//* Add shortcode
add_shortcode( 'portfolio-carousel' , 'carouselSc' );
/**
* Enque styles and scripts.
*/
function enqueStuff() {
//* Owl carousel
wp_enqueue_style( 'owl.carousel', get_bloginfo( 'stylesheet_directory' ) . '/owl.carousel/assets/owl.carousel.css' );
wp_enqueue_script( 'owl.carousel.min', get_bloginfo( 'stylesheet_directory' ) . '/owl.carousel/owl.carousel.min.js', array( 'jquery' ) );
wp_enqueue_script( 'owl_init', get_bloginfo( 'stylesheet_directory' ) . '/js/owl_init.js', array( 'jquery' ) );
wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css' );
}
/**
* Creates shortcode for the carousel.
*/
function carouselSc($atts){
$args = array(
'post_type' => 'portfolio',
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ){
//loop through portfolio items
$content .= "<div class='owl-carousel'>";
while( $loop->have_posts() ): $loop->the_post();
if( has_post_thumbnail( ) ){
$url = wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) );
$content .= "<div class='item'>";
$content .= "<div style='background-image: url(" . $url . ");'>";
$content .= "</div>";
$content .= "<div class='lp-caption-carousel'>";
$content .= "<h3>" . get_the_title() . "</h3>";
$content .= "<p>" . substr(get_the_excerpt(), 0, 30) . "</p>";
$content .= "<a class='' href='" . get_the_permalink() . "'>[ Read More ]</a>";
$content .= "</div>";
$content .= "</div>";
}
endwhile;
$content .= "</div>";
}
return $content;
}
// initiate owl
jQuery(document).ready(function($){
$('.owl-carousel').owlCarousel({
rtl:true,
loop:true,
margin:0,
nav:true,
responsive:{
0:{
items:1
},
600:{
items:3
},
1000:{
items:5
}
}
})
})
/*-----------------------------------------------------------------------------------*
/* Portfolio carousel
/*-----------------------------------------------------------------------------------*/
.lp-caption-carousel {
background-color: rgba(0, 0, 0, 0);
visibility: hidden;
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
text-align: center;
color: #fff;
-moz-transition: all .2s ease-in;
-o-transition: all .2s ease-in;
-webkit-transition: all .2s ease-in;
transition: all .2s ease-in;
padding: 15px;
}
div.item:hover div {
background-color: rgba(0, 0, 0, 0.5);
visibility: visible;
}
.item div{
background-size: cover;
background-position: center center;
height: 200px;
}
.item h3, .item a{
color: #fff;
}
.owl-nav div {
position: absolute;
top: 40%;
width: 50px;
height: 50px;
border-radius: 100%;
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
background-color: #3d91f0;
text-indent: -9999px;
overflow: hidden;
}
.owl-next {
right: 25px;
}
.owl-prev {
left: 25px;
}
.owl-next:after {
content: "\f105";
text-indent: 15px;
}
.owl-prev:after {
content: "\f104";
text-indent: 20px;
}
.owl-nav div:after {
font: normal normal normal 14px/1 FontAwesome;
font-size: 40px;
display: block;
color: #fff;
margin-top: -25px;
}
@media only screen and (max-width: 768px) {
.lp-caption-carousel {
background-color: rgba(0, 0, 0, 1);
visibility: visible;
position: static;
text-align: center;
color: #fff;
transition: all .2s ease-in;
height: 92px !important;
display: block;
}
.lp-caption-carousel p {
display: none;
}
div.item:hover div {
background-color: rgba(0, 0, 0, 1);
}
}
@media (max-width:1201px) and (min-width:960px) {
.lp-caption-carousel p {
display: none;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment