2017-03-03 66 views

回答

1

有两种方法可以在主题中添加碳字段插件。 1)在插件文件夹中添加碳字段插件并激活。 2)在主题文件夹中添加碳字段插件文件夹。

/* Integrating Carbon Fields Plugin */ 
    use Carbon_Fields\Container; 
    use Carbon_Fields\Field; 
    add_action('after_setup_theme', 'crb_setup_theme'); 
    function crb_setup_theme() { 
     // check if Carbon Fields is not already installed 
     if (! class_exists("\\Carbon_Fields\\Field\\Field")) { 
      require(get_template_directory() . '/carbon-fields/carbon-fields-plugin.php'); 
     } 
    } 
    add_action('carbon_register_fields', 'crb_register_custom_fields'); 
    function crb_register_custom_fields() { 
     require get_template_directory() . '/theme_option/meta_boxes.php'; 
    } 

在meta_boxes.php文件中,您需要创建metafield。像这样

use Carbon_Fields\Container; 
    use Carbon_Fields\Field; 


    Container::make('post_meta', 'Date Container')             // New Added 
    ->show_on_post_type(array('hotel')) // Hotel is custom post type 
    ->add_fields(array(
      Field::make('complex','hotel_details_page_content', 'Images & Content') 
      ->add_fields('Entry Container', array(
      Field::make("separator", "crb_image_options", "Image Entry"), 
      Field::make("image", "row_images", "Image"), 
      Field::make("text", "row_image_headlines", "Image Headline"), 
      Field::make("text", "image_row_subheadlines", "Image Sub Headline"), 
      Field::make("separator", "crb_style_options", "Content Entry"), 
      Field::make("text", "content_titles", "Title"), 
      Field::make("rich_text", "content_datas", "Content"), 
     )), 
    )); 

    Container::make('post_meta', 'Gallery Container') 
     ->show_on_post_type('gallery') // Gallery is custom post type 
     ->add_fields(array(
     Field::make('complex','galleryimages', 'Gallery') 
      ->add_fields('Gallery Entry', array(
      Field::make("image", "gallery_images", "Upload Image"), 
     )) 
    )); 

通过这种方式,您可以创建多个字段作为中继器和值也保存。

并在前端获取值。

$carbvalue= carbon_get_post_meta(get_the_ID(), 'hotel_details_page_content', 'complex'); 

foreach ($carbvalue as $carbonvalues) { 
    $herorimageid = $carbonvalues['row_images']; 

    $heroimage = wp_get_attachment_url($herorimageid); 
    ?> 
    <img src="<?php echo $heroimage; ?>" sizes="100vw" alt="heroimage" /> 

    <?php 
    echo $carbonvalues['row_image_headlines']; 
}