Wordpress可重复的metabox组

时间:2014-08-26 13:24:21

标签: php wordpress

我在page.php中使用什么代码spinet来显示前端的数据。

array(
'id'          => $prefix . 'repeat_group',
'type'        => 'group',
'description' => __( 'Generates reusable form entries', 'cmb' ),
'options'     => array(
    'group_title'   => __( 'Entry {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number
    'add_button'    => __( 'Add Another Entry', 'cmb' ),
    'remove_button' => __( 'Remove Entry', 'cmb' ),
    'sortable'      => true, // beta
),
// Fields array works the same, except id's only need to be unique for this group. Prefix is not needed.
'fields'      => array(
    array(
        'name' => 'Entry Title',
        'id'   => 'title',
        'type' => 'text',
        // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
    ),
    array(
        'name' => 'Description',
        'description' => 'Write a short description for this entry',
        'id'   => 'description',
        'type' => 'textarea_small',
    ),
    array(
        'name' => 'Entry Image',
        'id'   => 'image',
        'type' => 'file',
    ),
    array(
        'name' => 'Image Caption',
        'id'   => 'image_caption',
        'type' => 'text',
    ),
),

),

以上代码取自:

https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Field-Types#group

我没有找到任何方法将它放在page.php中并在前端页面显示数据。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我认为在WordPress管理员中使用可重复元变量的最简单方法是使用ACF(http://www.advancedcustomfields.com/)和转发器字段(http://www.advancedcustomfields.com/add-ons/repeater-field/

它提供了一个非常好的最终产品,它易于使用/实现,它具有良好的支持,并且可以与任何主题/插件捆绑在一起,只要您按照说明进行操作。

答案 1 :(得分:0)

我可以确认您的可重复群组设置与CMB2完美配合(注意一些增强功能),请在Github上查看以获取更多详情。

您提供的链接指的是旧版本的插件,现在名为CMB2,它确实是WordPress的一个很棒的自定义字段插件,您可以像任何普通的插件一样安装和使用它,或者您可以在其中引导它你自己的主题。

您提到您的主题似乎已经有了另一个字段,而不是您要设置的字段,对吧?如果这是真的,这会让我认为您的主题可能需要(包括).php文件某处负责启动CMB2和自定义字段设置。它也可能发生在您主题的functions.php文件中。同样,如果是这种情况,放置您提供的代码的正确位置是您需要识别的特定文件,而不是page.php模板。

当您使用CMB2设置自定义字段和字段组时,您可以定义字段可用的对象类型数组,它可以是页面,帖子,帖子类型甚至是分类术语。

我将在这里引用我自己的用例,以举例说明设置的字段。我使用以下代码显示一组字段,其中包含restaurant_menu类型的所有帖子的定价选项,但您可以自定义字段提供支持的对​​象。例如:'object_types' => array( 'page, post, post_type' )

- // -

    /**
     * Example Repeatable Group - Pricing Options Custom Fields // with Repeatable Fields within the group
     */               
    $cmb_group = new_cmb2_box( array(
      'id'            => $prefix . 'example_price_box',
      'title'         => __( 'Pricing Options', 'mytheme' ),
      'object_types'  => array( 'restaurant_menu' ),
    ) );

    // Parent
    $group_field_id = $cmb_group->add_field( array(
      'id'          => $prefix . 'example_price_group',
      'type'        => 'group',
      'options'     => array(
      'group_title'   => __( 'Price Box {#}', 'mytheme' ), // translatable 
      'add_button'    => __( 'Add New Price Box', 'mytheme' ),
      'remove_button' => __( 'Remove Price Box', 'mytheme' ),
      'sortable'      => true, // beta
    ),

    ) );

    // Price Label
    $cmb_group->add_group_field( $group_field_id, array(
      'name' => __( 'Price Legend', 'pivot'),
      'desc' => __( 'Set a short legend for the price. e.g Small, Large, 4 slices, 500g etc.', 'my theme'),
      'id'   => 'price_title',
      'type' => 'text_small',
      'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
      'options' => array( 'add_row_text' => __( 'Add New Legend', 'mytheme' ) ),
    ) );

    // Price Label Description
    $cmb_group->add_group_field( $group_field_id, array(
      'name' => __( 'Price Description', 'mytheme' ),
      'desc' => __( 'A very short description for the price legend.', 'mytheme'),
      'id'   => 'price_desc',
      'type' => 'text_medium',
      'after_field'  => __( '<p><b>E.g.</b> "Serves up to 4 people", "Made with Premium Cheese" etc.</p>', 'mytheme'),
      //'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
      //'show_in_rest'  => true, // expose field to WP REST API / Under CMB2 Development - please check for CMB2 on Github
      //'show_in_rest'  => array( 'read_and_write', 'write_only' ), true, // expose field to WP REST API using an array of permission types
    ) );          

    // Price Amount
    $cmb_group->add_group_field( $group_field_id, array(
      'name' => __( 'Product Price', 'mytheme' ),
      'desc' => __( 'Set the price for the item. Use , to separate decimals.', 'pivot'),
      'id'   => 'price_tag',
      'type' => 'text_money',
      'before_field' => '$', // override '$' symbol if needed
      //'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
    ) );

至于现在CMB2支持很多字段类型(包括可重复的字段和组),它有几十个第三方添加(扩展)可供选择,其中大部分是完全免费的。维护得很好,设置起来非常简单。我在我的主题上使用它,我甚至不想在短期内更换它。

相关问题