Last active
September 4, 2015 12:25
-
-
Save partageit/50780c595396a572a3f9 to your computer and use it in GitHub Desktop.
Partage-it.com : Carrousel d'articles pour WordPress avec Twitter Bootstrap
This file contains 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 bootstrap, in order to be able to use its carousel feature | |
function enqueueStyles() { | |
wp_enqueue_style("bootstrap", get_stylesheet_directory_uri() . "/css/bootstrap/css/bootstrap.min.css"); | |
wp_enqueue_script('jquery'); | |
wp_enqueue_script('bootstrap-js', get_stylesheet_directory_uri() . '/js/bootstrap.min.js', 'jquery' ); | |
} | |
add_action("wp_enqueue_scripts", "enqueueStyles"); | |
/** | |
* Display a carousel of posts, according to the provided categories or tags | |
* Twitter Bootstrap JavaScript is needed. | |
* @var array $criterias List of criterias, to be passed to WP_Query. If empty, every posts are retreived. | |
* @var string $classes Classes added to the carousel | |
* @var string $id The carousel HTML identifier. If empty, the id is bsSlider (it is useful when many sliders are joyfully on the same page). | |
* @var int $active The active slide when starting. THe first slide is 1. | |
* @var string $messageIfEmpty The message replacing the slider if no posts match | |
* @var string $link The link of the slide. May be none (i.e. no link), post (i.e. a link to the post) or any relative or absolute URL or anchor | |
* @var bool $display If true, the carousel HTML content is displayed, else it is returned | |
* @return void|string The carousel HTML content if $display is true | |
* @see http://codex.wordpress.org/Class_Reference/WP_Query | |
*/ | |
function bootstrapCarousel($criterias = null, $classes = null, $id = "", $active = 1, $messageIfEmpty = "", $link = "post", $display = true) { | |
$result = ""; | |
$posts = array(); | |
if (!$criterias) $criterias = array("post_type" => "post"); | |
$query = new WP_Query($criterias); | |
if ($query->have_posts()) { | |
while ($query->have_posts()) { | |
$query->the_post(); | |
$posts[] = array( | |
"id" => $query->post->ID, | |
"permalink" => get_permalink($query->post->ID), | |
"title" => $query->post->post_title, | |
"excerpt" => $query->post->post_excerpt, | |
"thumbnail" => get_the_post_thumbnail($query->post->ID , "full") | |
); | |
} | |
} | |
wp_reset_postdata(); | |
$result = ""; | |
if (count($posts) === 0) { | |
$result = $messageIfEmpty; | |
} else { | |
if ($id === null) $id = "bsSlider"; | |
$result = "<div id=\"$id\" class=\"carousel slide $classes\" data-ride=\"carousel\">"; | |
$result .= " <ol class=\"carousel-indicators\">"; | |
foreach ($posts as $k => $post) { | |
$result .= " <li data-target=\"#$id\" data-slide-to=\"$k\""; | |
if ($active == $k + 1) $result .= " class=\"active\""; | |
$result .= "></li>"; | |
} | |
$result .= " </ol>"; | |
$result .= " <div class=\"carousel-inner\">"; | |
foreach ($posts as $k => $post) { | |
if ($link == "none") | |
$url = ""; | |
elseif ($link == "post") | |
$url = $post["permalink"]; | |
else | |
$url = htmlentities($link); | |
$result .= " <div class=\"item"; | |
if ($active == $k + 1) $result .= " active"; | |
$result .= "\">"; | |
if ($url) $result .= " <a href=\"$url\">"; | |
$result .= " " . $post["thumbnail"]; | |
$result .= " <div class=\"carousel-caption\">"; | |
$result .= " <h3>" . $post["title"] . "</h3>"; | |
$result .= " <p>" . $post["excerpt"] . "</p>"; | |
$result .= " </div>"; | |
if ($url) $result .= " </a>"; | |
$result .= " </div>"; | |
} | |
$result .= " </div>"; | |
$result .= " <a class=\"left carousel-control\" href=\"#$id\" data-slide=\"prev\"><span class=\"glyphicon glyphicon-chevron-left\"></span></a>"; | |
$result .= " <a class=\"right carousel-control\" href=\"#$id\" data-slide=\"next\"><span class=\"glyphicon glyphicon-chevron-right\"></span></a>"; | |
$result .= "</div>"; | |
} | |
if ($display) | |
echo $result; | |
else | |
return $result; | |
} | |
/** | |
* The carousel shortcode | |
* Examples: | |
* - [carousel categories="my-categorie-slug,my-other-category-slug"] | |
* - [carousel tags="my-tag-slug" active="2" messageIfEmpty="No posts for this tag"] | |
* - [carousel tags="my-tag-slug" link="/my-tag-page/"] | |
* @param array $attr Attributes of the shortcode. | |
* @param content $content Content of the shortcode. Not used here. | |
* @return string HTML content to display gallery. | |
*/ | |
function bootstrapCarouselShortcode($attributes, $content) { | |
extract(shortcode_atts(array( | |
"categories" => null, | |
"tags" => null, | |
"classes" => null, | |
"id" => null, | |
"active" => 1, | |
"messageIfEmpty" => "", | |
"link" => "post" | |
), $attributes)); | |
$criterias = array("post_type" => "post"); | |
if ($categories) $criterias["category_name"] = $categories; | |
if ($tags) $criterias["tag"] = $tags; | |
return bootstrapCarousel($criterias, $classes, $id, $active, $messageIfEmpty, $link, false); | |
} | |
add_shortcode("carousel", "bootstrapCarouselShortcode"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"excerpt" => get_the_excerpt(), ( marche pour moi avec cette modification. )
"excerpt" => $query->post->post_excerpt, ( me donnait des balises p vides. )