【问题标题】:WordPress Illegal string offset errorWordPress非法字符串偏移错误
【发布时间】:2018-02-27 22:30:38
【问题描述】:

我有一个为 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>

【问题讨论】:

  • 第 69 行是哪一行?
  • 第 69 行是
  • 你能不能var_dump($meta); 并用它的输出更新你的问题。
  • 你可以用 isset 包装它。 &lt;?php echo isset($meta['Title']) ? $meta['Title'] : ''; ?&gt;
  • 如果这是您遇到的错误,基本上 $meta 不能是数组。

标签: php wordpress post


【解决方案1】:

感谢凯青,这是对我有用的回应

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多