Adding custom field support?
-
Hey Phil.
I really like the CPT Carousel, but I need to add a feature small feature. I want tot have a button on the individual carousel items, which I can link to either an external or internal url. I think I am pretty close to solving it, but I don′t quite understand your loop.
I added custom field to the $args / supports
'supports' => array('title','excerpt','thumbnail', 'page-attributes', 'custom-fields')
I called the key link and the value is any url set from the backend.I updated the loop:
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' ); $link = get_post_meta($post->ID,'link',true); $images[] = array('title' => $title, 'content' => $content, 'image' => $image, 'link' => $link); } } if(count($images) > 0){ ob_start(); ?>
I also updated the output to this:
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> <?php echo '<a class="btn btn-large btn-primary" href="' . $image['link'] . '">L?s mer</a>';?> </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_<?php echo $id; ?>" data-slide="next">?</a> <?php } ?> </div> <?php } $output = ob_get_contents(); ob_end_clean();
I still can′t get the href to be set from the backend, where I think there is something wrong with the loop.
For full code please see: https://wordpress.stackexchange.com/questions/108369/echo-custom-field-value?noredirect=1#comment150918_108369https://www.ads-software.com/plugins/cpt-bootstrap-carousel/
- The topic ‘Adding custom field support?’ is closed to new replies.