Bootstrap 3.0 Carousel gallery post WordPress I have working code to show in index.php and content-gallery.php of WordPress with Bootstrap 3.0 Carousel.

But, I know it's not clean code.  Could anyone help me clean/improve it?

This is the code we need to generate Bootstrap Carousel: 

&lt;div id="carousel-example-generic" class="carousel slide"&gt;
  &lt;!-- Indicators --&gt;
  &lt;ol class="carousel-indicators"&gt;
    &lt;li data-target="#carousel-example-generic" data-slide-to="0" class="active"&gt;&lt;/li&gt;
    &lt;li data-target="#carousel-example-generic" data-slide-to="1"&gt;&lt;/li&gt;
    &lt;li data-target="#carousel-example-generic" data-slide-to="2"&gt;&lt;/li&gt;
  &lt;/ol&gt;

  &lt;!-- Wrapper for slides --&gt;
  &lt;div class="carousel-inner"&gt;
    &lt;div class="item active"&gt;
      &lt;img src="..." alt="..."&gt;
      &lt;div class="carousel-caption"&gt;
        ...
      &lt;/div&gt;
    &lt;/div&gt;
    ...
  &lt;/div&gt;

  &lt;!-- Controls --&gt;
  &lt;a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"&gt;
    &lt;span class="icon-prev"&gt;&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class="right carousel-control" href="#carousel-example-generic" data-slide="next"&gt;
    &lt;span class="icon-next"&gt;&lt;/span&gt;
  &lt;/a&gt;
&lt;/div&gt;


This is the content of my content-gallery-php:

&lt;!-- Carousel
================================================== --&gt;
&lt;div id="carousel-example-generic" class="carousel slide"&gt;


 &lt;?php function_indicators($post) ?&gt;

 &lt;div class="carousel-inner"&gt;
 &lt;?php function_slides($post) ?&gt;
 &lt;/div&gt;
  &lt;!-- Controls --&gt;
  &lt;a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"&gt;
    &lt;span class="icon-prev"&gt;&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class="right carousel-control" href="#carousel-example-generic" data-slide="next"&gt;
    &lt;span class="icon-next"&gt;&lt;/span&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;!-- /.carousel --&gt;


And here are 3 functions to my functions.php:

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' =&gt; get_post_meta( $attachment-&gt;ID, '_wp_attachment_image_alt', true ),
        'caption' =&gt; $attachment-&gt;post_excerpt,
        'description' =&gt; $attachment-&gt;post_content,
        'href' =&gt; get_permalink( $attachment-&gt;ID ),
        'src' =&gt; $attachment-&gt;guid,
        'title' =&gt; $attachment-&gt;post_title
    );
}

function functions_indicators() {
$special_gallery = get_post_gallery( $post, false );
$ids = explode( ",", $special_gallery['ids'] );
$html = '&lt;ol class="carousel-indicators"&gt;';
foreach( $ids as $id ) {
    $link   = wp_get_attachment_url( $id );
    $class = ( $i == 0 ) ? 'active ' : '';
    $i++;
    $b=1;
    $html .= '&lt;li data-target="#carousel-example-generic" data-slide-to="'.($i - $b).'" '. 'class="'.$class.'"&gt;&lt;/li&gt;';
} 
$html .= '&lt;/ol&gt;';
echo $html;


}

function function_slides() {
$special_gallery = get_post_gallery( $post, false );
$ids = explode( ",", $special_gallery['ids'] );
$html = '&lt;div class="carousel-inner"&gt;';
foreach( $ids as $id ) {
    $link   = wp_get_attachment_url( $id );
    $attachment_meta = wp_get_attachment($id);
    $class = ( $i == 0 ) ? 'active ' : '';
    $i++;
    $html .= '&lt;div class="item '.$class. '"&gt;&lt;img src="' . $link . '"&gt;' . '&lt;div class="carousel-caption"&gt;&lt;h4&gt;'.$attachment_meta['title'].'&lt;/h4&gt;&lt;p&gt;'.$attachment_meta['description']. '&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;';
} 
$html .= '&lt;/div&gt;';
echo $html;}
IMO, clean wp code is hard, if not impossible to acchieve. I can tell you, however, that the first thing I'd remove from your functions is the echo $html; statements.

A function is a block of code that does something, and returns that data, to be used anywhere. Your functions don't return anything (apart from the implicit null), but echo a heck of a lot. That could be considered a side-effect. Functions that have side-effects are bad news. Why not replace the echo $html; with a simple return $html; for a start, then call the functions like so:

&lt;?=function_slides();?&gt;


&lt;?= is short for &lt;?php echo, and doesn't require the short_tags to be enabled anyway.
This is only the tip of the ice-berg, though. Your functions contain an awful lot of duplicate statements ($special_gallery = get_post_gallery( $post, false ), for one). Why not pass that as an argument, so you can keep it in scope for all calls, and avoid the overhead of repeating the same function calls?
What's more: the entire function bodies of both function_slides and function_indicators are remarkably similar, and could easily be combined into a single function:

function functions_slide_indicator($post) //specify argument, you forgot this
{
    $special_gallery = get_post_gallery( $post, false );
    $ids = explode( ",", $special_gallery['ids'] );
    $formats = array(
        'indicator' =&gt; '&lt;li data-target="#carousel-example-generic" data-slide-to="%d"&gt;&lt;/li&gt;',
        'slide'     =&gt; '&lt;div class="item"&gt;&lt;img src="%s"&gt;&lt;div class="carousel-caption"&gt;&lt;h4&gt;%s&lt;/h4&gt;&lt;p&gt;%s&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;'
    );
    $html = array(
        'indicator' =&gt; '&lt;ol class="carousel-indicators"&gt;&lt;li data-target="#carousel-example-generic" data-slide-to="0" class="active"&gt;&lt;/li&gt;',
        'slide'     =&gt; '&lt;div class="carousel-inner"&gt;'
    );
    $link= wp_get_attachment_url($ids[0]);//get data for first id, not in loop
    $attachment_meta = wp_get_attachment($ids[0]);
    $html['slide'] .= sprintf(str_replace('&lt;div class="item"', '&lt;div class="active item"', $formats['slide']), $link, $attachment_meta['title'], $attachment_meta['description']);
    for($i = 1, $j = count($ids);$i&lt;$j;$i++)
    {//start at 1, not 0, so no active class in here
        $link   = wp_get_attachment_url( $ids[$i] );
        $attachment_meta = wp_get_attachment($ids[$i]);
        $html['indicator'] .= sprintf($formats['indicator'], $i);// no need to increment/decrement it here, it'll be the value you need
        $html['slide'] .= sprintf($formats['slide'],$link, $attachment_meta['title'], $attachment_meta['description']);
    }
    $html['indicator'] .= '&lt;/ol&gt;';
    $html['slide'] .= '&lt;/div&gt;';//close markup
    return $html;//return array
}


Call this like so:

&lt;?php
    $content = functions_slide_indicator($post);
    echo $content['indicator'];//indicators
    echo $content['slide'];//carousel


Of course, this is only the start, instead of calling that function in the loop, I'd personally go for:

$links = array_map($ids, 'wp_get_attachment_url');


For example. Though this isn't the most performant way to go, at least it looks cleaner, though it could look cleaner still, if you used Objects.