Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to modify the CPT Bootstrap Carousel plugin, so that I can add a link button to the carousel items. The plugin creates a custom post type called cptbc_post_type, this post type is basically a gallery which you can display by adding a shortcode. What I want to do is to add a button with a href value that can be set from the backend. The post already supports the title and an excerpt to be set from the backend. I kind of understand where I need to add the code but I can´t figure it out. Any help would be appreciated.

The code:

<?php

// 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')
); 
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');

// FRONT END

// 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() ) {
    $loop->the_post();
    if ( '' != get_the_post_thumbnail() ) {
        $title = get_the_title();
        $content = get_the_excerpt();
        $image = get_the_post_thumbnail( get_the_ID(), 'full' );
        $images[] = array('title' => $title, 'content' => $content, 'image' => $image);
    }
}
if(count($images) > 0){
    ob_start();
    ?>
    <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 } ?>
        </ol>
        <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>
                    </div>
                <?php } ?>
            </div>
        <?php } ?>
        </div>
        <?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_<?phpecho $id; ?>" data-slide="next">›</a>
        <?php } ?>
    </div>
<?php }
$output = ob_get_contents();
ob_end_clean();

// Restore original Post Data
wp_reset_postdata();    

return $output;
}

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

?> 
share|improve this question
    
possible duplicate of Echo custom field value –  tallphil Jul 30 '13 at 22:28
add comment

2 Answers 2

up vote 1 down vote accepted

this would help you do that.. http://wordpress.org/plugins/advanced-custom-fields/ then you can set up the custom field to only show on cptbc_post_type only..

on the template you can then get the value you set on the admin like this:

<a href="<?php echo get_field('field_name', $post->ID);?>">Link</a>
share|improve this answer
    
Cool man il try it out! –  StenW Jul 29 '13 at 22:29
add comment

You can use Custom Content Type Manager plugin, it provides feature to create custom post types from admin and add custom fields to any post type ( check plugin )

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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