如何将自定义字段添加到自定义帖子类型?

问题描述 投票:5回答:3

早上好。

我创建了一个自定义帖子类型,称为“产品”。我想创建一个自定义字段(是metabox正确的字词?),我的客户可以在其中打勾,以确定是否在其中指定的帖子CPT是特色文章。

这是我的functions.php中的代码,用于创建“产品” CPT:

function products_custom_init() {

    $labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Product', 'post type singular name'),
        'add_new' => _x('Add New', 'products'),
        'add_new_item' => __('Add New Product'),
        'edit_item' => __('Edit Product'),
        'new_item' => __('New Product'),
        'view_item' => __('View Product'),
        'search_items' => __('Search Products'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => false,
        'query_var' => true,
        'rewrite' => array('slug','pages'),
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_position' => 5,
        'supports' => array('title','editor','thumbnail','excerpt',)
      );

    register_post_type( 'products' , $args );
}
add_action( 'init', 'products_custom_init' );

因此,如何将'功能'元框/自定义字段添加到only产品信息?

非常感谢,

辛西娅

wordpress custom-fields custom-post-type featured
3个回答
1
投票

7
投票

正如Muhammad Yasin所说,我建议使用一些插件:http://wordpress.org/extend/plugins/more-fields/

如果您想自己用代码做,请看:add_meta_box

add_meta_box

您可以为每个帖子类型注册邮箱。


0
投票

如果要在自定义帖子类型内创建自定义元框,则需要使用3个功能。

  1. 用于在帖子编辑屏幕上创建自定义“元框”块/屏幕的功能:<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>

  2. 用于添加输入字段以更改/显示您的自定义meta的功能

  3. 最后是一个功能,当您在帖子编辑屏幕上单击“保存”时,将元信息与其余帖子一起保存:add_meta_boxes_{$post_type}

在您的情况下,自定义复选框将如下所示:

add_meta_boxes_{$post_type}
© www.soinside.com 2019 - 2024. All rights reserved.