WordPress非法字符串偏移错误

时间:2018-02-27 22:30:39

标签: php wordpress post

我有一个我为WordPress网站构建的自定义帖子类型。除非我添加新帖子,否则所有功能都应该正常运行,自定义元字段会出现错误。

<br /><b>Warning</b>:  Illegal string offset 'Title' in <b>D:\Xampp\htdocs\wpexercise\wordpress\wp-content\themes\wpexercise\functions.php</b> on line <b>69</b><br />

我能够从输入字段中删除错误并输入数据并保存,但为什么会出现此错误。请帮忙。

的functions.php

function create_post_products_post() {
register_post_type( 'products_post',
    array(
        'labels'       => array(
            'name'       => __( 'Products' ),
        ),
        'public'       => true,
        'hierarchical' => true,
        'has_archive'  => true,
        'supports'     => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
        ),
        'taxonomies'   => array(
            'post_tag',
            'category',
        )
    )
);
register_taxonomy_for_object_type( 'category', 'products_post' );
register_taxonomy_for_object_type( 'post_tag', 'products_post' );
}
add_action( 'init', 'create_post_products_post' );



function add_products_fields_meta_box() {
add_meta_box(
    'products_fields_meta_box', // $id
    'Product Fields', // $title
    'show_products_fields_meta_box', // $callback
    'products_post', // $screen
    'normal', // $context
    'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_products_fields_meta_box' );

function show_products_fields_meta_box() {
global $post;
$meta = get_post_meta( $post->ID, $key='products_fields', true ); ?>

<input type="hidden" name="products_meta_box_nonce" value="<?php echo 
wp_create_nonce( basename(__FILE__) ); ?>">

<p>
    <label for="products_fields[Title]">Product Title</label>
    <br>
    <input type="text" name="products_fields[Title]" 
id="products_fields[textTitle]" class="regular-text" value="<?php echo 
$meta['Title']; ?>">
</p>

<p>
    <label for="products_fields[Link]">Product Link</label>
    <br>
    <input type="text" name="products_fields[Link]" id="products_fields[Link]" class="regular-text" value="<?php echo $meta['Link']; ?>">
</p>

<p>
    <label for="products_fields[bkgColor]">Background Color</label>
    <br>
    <input type="text" name="products_fields[bkgColor]" 
id="products_fields[bkgColor]" class="regular-text" value="<?php echo 
$meta['bkgColor']; ?>">
</p>

<p>
    <label for="products_fields[Image]">Product Image Path</label>
    <br>
    <input type="text" name="products_fields[Image]" 
id="products_fields[Image]" class="regular-text" value="<?php echo 
$meta['Image']; ?>">
</p>

1 个答案:

答案 0 :(得分:0)

感谢Kai Qing,这是对我有用的回复

<?php echo isset($meta['Title']) ? $meta['Title'] : ''; ?>