So I am customizing the [CPT Bootstrap Carousel plugin][1] to fit my needs. I have added custom field support to the custom post type, where the custom field is suppose to add a button on the carousel items. I have been able to register the custom field in the backend and the button is showing up on the Carousel items, but the href property is empty. I believe that the problem is in the custom loop or in the way I am using echo, but I can´t seam to figure it out. The area where I believe the problem in the code is have been market with <!--Possible problem-->.

Here is updated, functioning code: /PS: but have a look a phils answer, probably a more stable solution.

Plugin Name: CPT Bootstrap Carousel
Plugin URI: http://www.tallphil.co.uk/bootstrap-carousel
Description: A custom post type for choosing images and content which outputs <a href="http://twitter.github.io/bootstrap/javascript.html#carousel" target="_blank">Bootstrap Carousel</a> from a shortcode. Requires Bootstrap javascript and CSS to be loaded separately.
Version: 1.1
Author: Phil Ewels
Author URI: http://phil.ewels.co.uk
License: GPLv2

// Custom Post Type Setup
add_action( 'init', 'cptbc_post_type' );
function cptbc_post_type() {
$labels = array(
    'name' => 'Carousel Images',
    'singular_name' => 'Carousel Image',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Carousel Image',
    'edit_item' => 'Edit Carousel Image',
    'new_item' => 'New Carousel Image',
    'view_item' => 'View Carousel Image',
    'search_items' => 'Search Carousel Images',
    'not_found' =>  'No Carousel Image',
    'not_found_in_trash' => 'No Carousel Images found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Carousel'
$args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => true,
    'publicly_queryable' => false,
    'show_ui' => true, 
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'page',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => 21,
    'supports' => array('title','excerpt','thumbnail', 'page-attributes', 'custom-fields')
register_post_type('cptbc', $args);

// Add theme support for featured images if not already present
// http://wordpress.stackexchange.com/questions/23839/using-add-theme-support-inside-a-plugin
function cptbc_addFeaturedImageSupport() {
$supportedTypes = get_theme_support( 'post-thumbnails' );
if( $supportedTypes === false )
    add_theme_support( 'post-thumbnails', array( 'cptbc' ) );               
elseif( is_array( $supportedTypes ) ) {
    $supportedTypes[0][] = 'cptbc';
    add_theme_support( 'post-thumbnails', $supportedTypes[0] );
add_action( 'after_setup_theme', 'cptbc_addFeaturedImageSupport');


// Shortcode
function cptbc_shortcode($atts, $content = null) {
// Set default shortcode attributes
$defaults = array(
    'interval' => '5000',
    'showcaption' => 'true',
    'showcontrols' => 'true'

// Parse incomming $atts into an array and merge it with $defaults
$atts = shortcode_atts($defaults, $atts);

return cptbc_frontend($atts);
add_shortcode('image-carousel', 'cptbc_shortcode');

// Display latest WftC
function cptbc_frontend($atts){
$id = rand(0, 999); // use a random ID so that the CSS IDs work with multiple on one page
$args = array( 'post_type' => 'cptbc', 'orderby' => 'menu_order', 'order' => 'ASC');
$loop = new WP_Query( $args );
$images = array();
while ( $loop->have_posts() ) {
    if ( '' != get_the_post_thumbnail() ) {
        $title = get_the_title();
        $content = get_the_excerpt();
        $image = get_the_post_thumbnail( get_the_ID(), 'full' );
        $link = get_post_custom_values('link');
        $images[] = array('title' => $title, 'content' => $content, 'image' => $image, 'link' => $link);
if(count($images) > 0){
    <div id="cptbc_<?php echo $id; ?>" class="carousel slide">
        <ol class="carousel-indicators">
        <?php foreach ($images as $key => $image) { ?>
            <li data-target="#cptbc_<?php echo $id; ?>" data-slide-to="<?php echo $key; ?>" data-interval="<?php echo $atts['interval']; ?>" <?php echo $key == 0 ? 'class="active"' : ''; ?>></li>
        <?php } ?>
        <div class="carousel-inner">
        <?php foreach ($images as $key => $image) { ?>
            <div class="item <?php echo $key == 0 ? 'active' : ''; ?>">
                <?php echo $image['image']; ?>
                <?php if($atts['showcaption'] === 'true') { ?>
                    <div class="carousel-caption">
                        <h2><?php echo $image['title']; ?></h2>
                        <p class="lead"><?php echo $image['content']; ?></p>
                        <?php echo '<a class="btn btn-large btn-primary" href="' . $image['link'][0] . '">Läs mer</a>';?>
                <?php } ?>
        <?php } ?>
        <?php if($atts['showcontrols'] === 'true') { ?>
            <a class="left carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="prev">‹</a>
            <a class="right carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="next">›</a>
        <?php } ?>
<?php }
$output = ob_get_contents();

// Restore original Post Data

return $output;

// Call the carousel in javascript, else it won't start scrolling on its own
function cptbc_footer_js() {
<script type="text/javascript">
add_action('wp_footer', 'cptbc_footer_js');

  • What is var_dump($link);? – s_ha_dum Jul 30 '13 at 19:27
  • when I do <?php var_dump($link);?> it get = bool(false)! – StenW Jul 30 '13 at 19:31
  • Where are you saving that link meta key? – s_ha_dum Jul 30 '13 at 19:35
  • 2
    where you output the link, it should be $image['link'] not $link['link'] – Milo Jul 30 '13 at 19:38
  • 1
    you're no longer in the context of that loop, you put the link in an array on the line $images[] = array(... under the key link. you then loop over that array to output everything: foreach ($images as $key => $image), so you need to reference that key in the $image variable. – Milo Jul 30 '13 at 20:03

Repost for any future googlers from @StenW 's wordpress plugin support thread:

Github user @atnon made a very similar modification to the code 10 days ago, also using custom-fields (though giving the image a link rather than putting a link in the caption). You can see this code in his pull request.

I re-wrote his code and released an update to the plugin last night, so if you're happy with just the image having a link you should be able to do that with the update.

If I were you, I'd update the plugin to incorporate my new code and then make a small edit just to change where the link is being printed in the output. That way you get a cleaner meta-box instead of relying on custom fields and it should work without much debugging.

Example modification (untested):

<div class="item <?php echo $key == 0 ? 'active' : ''; ?>">
    <?php echo $image['image'];
    if($atts['showcaption'] === 'true') { ?>
        <div class="carousel-caption">
            <h4><?php echo $image['title']; ?></h4>
            <p><?php echo $image['content']; ?></p>
            <?php if($image['url']) {
                echo '<a href="'.$image['url'].'"';
                if($image['url_openblank']) {
                    echo ' target="_blank"';
                echo '>Läs mer</a>';
            } ?>
    <?php } ?>
| improve this answer | |
  • Awesome phil, I came up with my own solution which might not be as elegant but it works. So now people have to options :) – StenW Jul 30 '13 at 22:23
  • Cool, glad it's working and glad you're finding the plugin useful! If you could upvote my answer / mark it as correct so that it floats to the top that'd be good.. – ewels Jul 30 '13 at 22:26
  • There should be a CPT-Boostrap-Carousel tag in [wordpress.stackexechange.com], but my rep is to low to create it... Should be taken care of. – StenW Jul 30 '13 at 22:38


$link = get_post_meta($post->ID, 'link', TRUE); // please, note true

If you use, instead

get_post_meta($post->ID, 'link', FALSE);

an array is returned so you should use

href="' . $image['link'][0] . '"

See the codex for get_post_meta

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.