Rilwis的Meta Box,在除主页外的所有页面模板上加载metabox

时间:2013-07-24 作者:Poisontonomes

使用Rilwis的Meta Box插件来定制metabox,有没有办法在所有页面模板上加载metabox,除了template-home.php?

\'only_on\' => array(
    \'template\' => array( \'template-home.php\' ),
),
我尝试添加! 之前,但它没有工作。

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

你可以翻转rw_maybe_include() 围绕并创建一个rw_maybe_exclude()

/**
 * Check if meta boxes is included
 *
 * @return bool
 */
function rw_maybe_exlude( $conditions ) {
    // Include in back-end only
    if ( ! defined( \'WP_ADMIN\' ) || ! WP_ADMIN ) {
        return true;
    }

    // Always include for ajax
    if ( defined( \'DOING_AJAX\' ) && DOING_AJAX ) {
        return false;
    }

    if ( isset( $_GET[\'post\'] ) ) {
        $post_id = $_GET[\'post\'];
    }
    elseif ( isset( $_POST[\'post_ID\'] ) ) {
        $post_id = $_POST[\'post_ID\'];
    }
    else {
        $post_id = false;
    }

    $post_id = (int) $post_id;
    $post    = get_post( $post_id );

    foreach ( $conditions as $cond => $v ) {
        // Catch non-arrays too
        if ( ! is_array( $v ) ) {
            $v = array( $v );
        }

        switch ( $cond ) {
            case \'id\':
                if ( in_array( $post_id, $v ) ) {
                    return true;
                }
            break;
            case \'parent\':
                $post_parent = $post->post_parent;
                if ( in_array( $post_parent, $v ) ) {
                    return true;
                }
            break;
            case \'slug\':
                $post_slug = $post->post_name;
                if ( in_array( $post_slug, $v ) ) {
                    return true;
                }
            break;
            case \'template\':
                $template = get_post_meta( $post_id, \'_wp_page_template\', true );
                if ( in_array( $template, $v ) ) {
                    return true;
                }
            break;
        }
    }

    // If no condition matched
    return false;
}
然后使用exclude_on 钥匙

$prefix = \'rw_\';

global $meta_boxes;

$meta_boxes = array();

$meta_boxes[] = array(
    \'title\'  => __( \'Meta Box Title\', \'rwmb\' ),
    \'pages\'    => array( \'post\', \'page\' ),
    \'fields\' => array(
        array(
            \'name\' => __( \'Your images\', \'rwmb\' ),
            \'id\'   => "{$prefix}img",
            \'type\' => \'plupload_image\',
        ),
    ),
    \'exclude_on\'    => array(
        \'template\' => array( \'template-home.php\' )
    ),
最后,根据新函数注册元数据库。

/**
 * Register meta boxes
 *
 * @return void
 */
function rw_register_meta_boxes()
{
    global $meta_boxes;

    // Make sure there\'s no errors when the plugin is deactivated or during upgrade
    if ( class_exists( \'RW_Meta_Box\' ) ) {
        foreach ( $meta_boxes as $meta_box ) {
            if ( isset( $meta_box[\'exclude_on\'] ) && rw_maybe_exlude( $meta_box[\'exclude_on\'] ) ) {
                continue;
            }

            new RW_Meta_Box( $meta_box );
        }
    }
}

add_action( \'admin_init\', \'rw_register_meta_boxes\' );

SO网友:Wyck

您可以使用主页检查,这可能比使用is_front_page, 这适用于设置为显示在主页上的静态页面。

如果没有,您可以尝试使用get_page_template 函数,然后匹配字符串。

$pagetemplate  = get_page_template();
if ( $pagetemplate == \'template-home.php\' )
    //do something
http://codex.wordpress.org/Function_Reference/is_front_page
http://codex.wordpress.org/Function_Reference/get_page_template

结束

相关推荐

Metabox现时值不允许更新

更正在自定义帖子类型上保存metabox数据时遇到问题。我想我通过在这篇帖子上应用建议来解决这个问题:Metabox nonce PHP notice这确实消除了警告,但使我无法更新元数据库。以下是相关代码:function save_goal_info_meta( $post_id ) { global $goal_info_meta_fields; // verify nonce if ( ! isset( $_POST[\'product_n