Echo custom field value

时间:2013-07-30 作者:StenW

因此,我正在定制[CPT引导Carousel插件][1],以满足我的需要。我已经为自定义帖子类型添加了自定义字段支持,其中自定义字段应该在旋转木马项目上添加一个按钮。我已经能够在后端注册自定义字段,并且按钮显示在旋转木马项目上,但是href 属性为空。我认为问题在于自定义循环或我使用的方式echo, 但我无法找到答案。我认为代码中存在问题的领域是<!--Possible problem-->.

下面是更新的功能代码:/PS:但看看phils的答案,可能是一个更稳定的解决方案。

<?php
/*
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\');

// 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\' );
        $link = get_post_custom_values(\'link\');
        $images[] = array(\'title\' => $title, \'content\' => $content, \'image\' => $image, \'link\' => $link);
    }
}
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>
                        <?php echo \'<a class="btn btn-large btn-primary" href="\' . $image[\'link\'][0] . \'">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();

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

?>

2 个回复
最合适的回答,由SO网友:ewels 整理而成

从@StenW的wordpress插件为任何未来的Googler用户重新发布support thread:

Github用户@atnon 10天前对代码进行了非常类似的修改,也使用了自定义字段(尽管为图像提供了链接,而不是在标题中添加链接)。你可以在他的pull request.

昨晚,我重新编写了他的代码,并发布了插件的更新,因此,如果您对只有图像有链接感到满意,那么您应该可以通过更新来做到这一点。

如果我是你,我会更新插件以合并我的新代码,然后进行一个小的编辑,以更改输出中打印链接的位置。这样,您就得到了一个更干净的元框,而不是依赖于自定义字段,它应该可以在不进行太多调试的情况下工作。

修改示例(未测试):

<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>\';
            } ?>
        </div>
    <?php } ?>
</div>

SO网友:gmazzap

使用:

$link = get_post_meta($post->ID, \'link\', TRUE); // please, note true
如果您使用

get_post_meta($post->ID, \'link\', FALSE);
返回了一个数组,因此您应该使用

href="\' . $image[\'link\'][0] . \'"
参见法典get_post_meta

结束

相关推荐