【问题标题】:Custom Meta Box for Testimonials Custom Post Type用于推荐的自定义元框自定义帖子类型
【发布时间】:2017-05-23 23:29:41
【问题描述】:

我有一个用于推荐的自定义帖子类型,其工作方式与我期望的一样。下面是推荐的sn-p代码

add_action( 'init', 'register_cpt_testimonial' );
function register_cpt_testimonial() {

    ...

    $args = array( 
        ..
    );

    register_post_type( 'testimonial', $args );
}

但是,现在我想花哨并添加元框,但我无法显示它。

function testimonial_meta_boxes() {
    add_meta_box( 'testimonial_form', 'Testimonial Details', 'testimonial_form', 'testimonial', 'side', 'high' );
}

function testimonial_form() {
    $post_id = get_the_ID();
    $testimonial_data = get_post_meta( $post_id, '_testimonial', true );
    $client_name = ( empty( $testimonial_data['client_name'] ) ) ? '' : $testimonial_data['client_name'];

    wp_nonce_field( 'testimonial', 'testimonial' );
    ?>
    <p>
        <label>Client's Name (optional)</label><br />
        <input type="text" value="<?php echo $client_name; ?>" name="testimonial[client_name]" size="40" />
    </p>
    <?php
}

有人可以解释一下我在这里缺少什么。我正在阅读https://developer.wordpress.org/reference/functions/add_meta_box/,但我只是不明白我所缺少的。

【问题讨论】:

  • 元框未显示在管理面板中?

标签: wordpress wordpress-theming


【解决方案1】:

你必须添加这个钩子add_action('add_meta_boxes', 'testimonial_meta_boxes');

add_action('add_meta_boxes', 'testimonial_meta_boxes');

function testimonial_meta_boxes() {
   add_meta_box( 'testimonial_form', 'Testimonial Details', 'testimonial_form', 'testimonial', 'side', 'high' );
}

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    相关资源
    最近更新 更多