如何添加href=“”自定义帖子类型?

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

我正在尝试修改CPT引导旋转木马插件,以便可以向旋转木马项目添加链接按钮。该插件创建了一个名为cptbc\\u post\\u type的自定义帖子类型,该帖子类型基本上是一个图库,您可以通过添加一个快捷码来显示它。我想做的是添加一个带有href值的按钮,该值可以从后端设置。该帖子已经支持从后端设置标题和摘录。我有点理解我需要在哪里添加代码,但我想不出来。任何帮助都将不胜感激。

代码:

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

?> 

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

这将有助于你做到这一点。。http://wordpress.org/plugins/advanced-custom-fields/然后可以将自定义字段设置为仅显示在cptbc_post_type 只有

然后,您可以在模板上获得在管理员上设置的值,如下所示:

<a href="<?php echo get_field(\'field_name\', $post->ID);?>">Link</a>

SO网友:Vikram

您可以使用Custom Content Type Manager plugin,它提供了从管理员创建自定义帖子类型的功能,并将自定义字段添加到任何帖子类型(check plugin )

结束

相关推荐