CMB2在我的WordPress主题中不起作用

时间:2017-07-31 作者:The WP Intermediate

我遵守了本手册中的说明blog 设置CMB2插件,但在我的情况下不起作用。

主题元功能。php→

    <?php
/**
 * Include and set up custom metaboxes and fields. (Make sure you copy this file outside the CMB2 directory)
 *
 * Be sure to replace all instances of \'yourprefix_\' with your project\'s prefix.
 * http://nacin.com/2010/05/11/in-wordpress-prefix-everything/
 *
 * @category YourThemeOrPlugin
 * @package  Demo_CMB2
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
 * @link     https://github.com/WebDevStudios/CMB2
 */

/**
 * Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
 */



add_action( \'cmb2_admin_init\', \'register_testimonial_metabox\' );
/**
 * Hook in and add a testimonial metabox. Can only happen on the \'cmb2_admin_init\' or \'cmb2_init\' hook.
 */
function register_testimonial_metabox() {

 // Start with an underscore to hide fields from custom fields list
    $prefix = \'_yourprefix_\'; //note, you can use anything you\'d like here

/**
 * Start field groups here
 */

// This first field group tells WordPress where to put the fields. In the example below, it is set to show up only on Post_ID=10

$cmb_demo = new_cmb2_box( array(
        \'id\'            => $prefix . \'metabox\',
        \'title\'         => __( \'Homepage Custom Fields\', \'cmb2\' ),
        \'object_types\'  => array( \'page\', ), // Post type
        \'show_on\'      => array( \'id\' => array( 10, ) ), // Specific post IDs to display this metabox
        ) );


$cmb_demo->add_field( array(
    \'name\' => __( \'Testimonial Author\', \'cmb2\' ),
    \'desc\' => __( \'Who is the testimonial from\', \'cmb2\' ),
    \'id\'   => $prefix . \'author\', //Note, I renamed this to be more appropriate
    \'type\' => \'textarea_small\',
    ) );

$cmb_demo->add_field( array(
    \'name\'    => __( \'Testimonial\', \'cmb2\' ),
    \'desc\'    => __( \'add the testimonial here\', \'cmb2\' ),
    \'id\'      => $prefix . \'testimonial\', //Note, I renamed this to be more appropriate
    \'type\'    => \'wysiwyg\',
    \'options\' => array( \'textarea_rows\' => 5, ),
    ) );

    $cmb_demo->add_field( array(
    \'name\' => __( \'Author Image\', \'cmb2\' ),
    \'desc\' => __( \'Upload an image or enter a URL.\', \'cmb2\' ),
    \'id\'   => $prefix . \'image\', //Note, I renamed this to be more appropriate
    \'type\' => \'file\',
    ) );
}
然后我在函数中包含了这样的内容。php→

require_once( dirname(__FILE__) . \'/inc/lib/theme-meta-functions.php\');
但是没有一个meta出现在帖子中→https://www.screencast.com/t/Bvihe62fZMV

我忘了提一下plugin is already installedWordpress.

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

解决方案→

\'object_types\'  => array( \'page\', ), // Post type
        \'show_on\'      => array( \'id\' => array( 10, ) ), // Specific post IDs to display this metabox
以上就是它不在岗位上工作的原因。

我刚刚添加了帖子并添加到这里→

\'object_types\'  => array( \'page\', ), // Post type
并删除了此行→

 \'show_on\'      => array( \'id\' => array( 10, ) ), // Specific post 
一切都开始起作用了,插件或任何代码都没有问题。

SO网友:Nuno Sarmento

改变$prefix = \'_yourprefix_\';$prefix = \'_wp\';

或者将下面的代码直接添加到函数中。php

add_action( \'cmb2_admin_init\', \'register_testimonial_metabox\' );

function register_testimonial_metabox() {

$prefix = \'_wp\'; 

$cmb_demo = new_cmb2_box( array(
\'id\'            => $prefix . \'metabox\',
\'title\'         => __( \'Homepage Custom Fields\', \'cmb2\' ),
\'object_types\'  => array( \'page\', ), // Post type
\'show_on\'      => array( \'id\' => array( 10, ) ),
) );


$cmb_demo->add_field( array(
\'name\' => __( \'Testimonial Author\', \'cmb2\' ),
\'desc\' => __( \'Who is the testimonial from\', \'cmb2\' ),
\'id\'   => $prefix . \'author\', 
) );

$cmb_demo->add_field( array(
\'name\'    => __( \'Testimonial\', \'cmb2\' ),
\'desc\'    => __( \'add the testimonial here\', \'cmb2\' ),
\'id\'      => $prefix . \'testimonial\',
\'type\'    => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 5, ),
) );

$cmb_demo->add_field( array(
\'name\' => __( \'Author Image\', \'cmb2\' ),
\'desc\' => __( \'Upload an image or enter a URL.\', \'cmb2\' ),
\'id\'   => $prefix . \'image\', 
\'type\' => \'file\',
) );
}

结束

相关推荐

Theme styling for plugins

我有一个插件,它有自己的CSS,用于在使用相关短代码时生成的内容。我正在尝试创建一个主题来重新设置我网站前端的样式,但由于这个插件有自己的CSS,我如何在新主题中修改它?