PHP - 警告:自定义 WordPress 主题中的非法字符串偏移
PHP - warning: Illegal string offset in custom WordPress theme
我正在尝试修复不是我编写的自定义 WordPress 主题。我有限的 PHP 知识无法解决这个问题。我详尽的搜索发现了类似的问题,大多数答案都说数组传递的是字符串或错误类型。我只是不知道如何修复它,或者我应该取消警告?
警告:第 39 行的非法字符串偏移量 'the_link'
警告:第 58 行
上的非法字符串偏移量 'subheader'
这是有问题的代码。任何帮助将不胜感激。
<?php get_header(); ?>
<?php wp_nav_menu( array( 'container_class' => 'header-bar','theme_location' => 'reviews' ) ); ?>
<div class="content wiemer-pages">
<?php the_post(); ?>
<div id="page-header-wrap">
<div id="page-header" class="no-shadow wreath">
<?php the_post_thumbnail(); ?>
<h1 class="page-title">
<?php the_title(); ?>
</h1>
<div id="page-header-content">
<?php $pageheader_metabox->the_meta();
$pageheader_metabox->the_value('title');?>
</div>
<div class="clear"></div>
</div>
</div>
<!-- [END] page-header-wrap + page header -->
<div class="page-content">
<?php the_content(); ?>
<?php edit_post_link(); ?>
</div>
<?php
$args=array(
'post_type'=>'reviews',
'posts_per_page'=>60
);
$catreview = new WP_Query($args);
global $post;
?>
<?php if($catreview->have_posts()): ?>
<h2 class="review-category" id="accolades">Recent Accolades </h2>
<?php while ($catreview->have_posts()) : $catreview->the_post();?>
<?php // load the meta
$mainurl='#';
$meta= get_post_meta($post->ID, '_urls', true);
$mainurl = $meta['the_link']; <!-- line 39 -->
?>
<div class="review-item">
<div class="review-thumb-container">
<?php // post thumbnail
if (has_post_thumbnail()){ ?>
<a class="review-thumbnail" href="<?php echo $mainurl ?>">
<?php the_post_thumbnail(array(144,180),array("title" => get_the_title($post->ID) . " - Thumbnail")); ?>
</a>
<?php } ?>
</div>
<div class="review-body">
<h6>
<?php the_time('F Y') ?>
<?php //echo get_the_term_list( $post->ID, 'accolades', '| '); ?>
</h6>
<h3><a href="<?php echo $mainurl; ?>">
<?php the_title() ?>
</a></h3>
<?php if ($sh = $meta['subheader']){ echo "<h4> $sh </h4>"; } ?> <!-- line 58 -->
<div class="review-content">
<?php the_content(); ?>
<?php edit_post_link( 'Edit', '<div class="edit-link">', '</div>' ); ?>
</div>
<?php if (isset($meta['link_title'])): ?>
<ul class="url-list">
<li> <a href="<?php echo $meta['the_link']; ?>" class="note-cat">
<?php echo $meta['link_title']; ?></a> </li>
</ul>
<?php endif; //urls ?>
</div>
</div>
<?php endwhile; ?>
<p><a class="to-top" title="Top of the page" href="#masthead">Top↑</a></p>
<?php endif; //End if for Accolades ?>
</div>
<?php get_footer(); ?>
我认为你的问题是 $meta
不是一个数组,因为 get_post_meta
当前 return 是单个值,因为第三个参数设置为 true。
Wordpress文档中第三个参数的说明:
$single
(bool) (Optional) Whether to return a single value.
Default value: false
您目前正在使用:
$meta = get_post_meta($post->ID, '_urls', true);
第三个参数设置为true。所以它将 return 一个单一的值(不是一个数组)。所以你可以将它设置为 false(或者删除第三个参数,因为默认值为 false)到 return 一个数组。
您可以在此处找到有关 get_post_meta
函数的更多信息:
https://developer.wordpress.org/reference/functions/get_post_meta/
关于 'Warning: Illegal string offset' 错误的信息:
我正在尝试修复不是我编写的自定义 WordPress 主题。我有限的 PHP 知识无法解决这个问题。我详尽的搜索发现了类似的问题,大多数答案都说数组传递的是字符串或错误类型。我只是不知道如何修复它,或者我应该取消警告?
警告:第 39 行的非法字符串偏移量 'the_link'
警告:第 58 行
这是有问题的代码。任何帮助将不胜感激。
<?php get_header(); ?>
<?php wp_nav_menu( array( 'container_class' => 'header-bar','theme_location' => 'reviews' ) ); ?>
<div class="content wiemer-pages">
<?php the_post(); ?>
<div id="page-header-wrap">
<div id="page-header" class="no-shadow wreath">
<?php the_post_thumbnail(); ?>
<h1 class="page-title">
<?php the_title(); ?>
</h1>
<div id="page-header-content">
<?php $pageheader_metabox->the_meta();
$pageheader_metabox->the_value('title');?>
</div>
<div class="clear"></div>
</div>
</div>
<!-- [END] page-header-wrap + page header -->
<div class="page-content">
<?php the_content(); ?>
<?php edit_post_link(); ?>
</div>
<?php
$args=array(
'post_type'=>'reviews',
'posts_per_page'=>60
);
$catreview = new WP_Query($args);
global $post;
?>
<?php if($catreview->have_posts()): ?>
<h2 class="review-category" id="accolades">Recent Accolades </h2>
<?php while ($catreview->have_posts()) : $catreview->the_post();?>
<?php // load the meta
$mainurl='#';
$meta= get_post_meta($post->ID, '_urls', true);
$mainurl = $meta['the_link']; <!-- line 39 -->
?>
<div class="review-item">
<div class="review-thumb-container">
<?php // post thumbnail
if (has_post_thumbnail()){ ?>
<a class="review-thumbnail" href="<?php echo $mainurl ?>">
<?php the_post_thumbnail(array(144,180),array("title" => get_the_title($post->ID) . " - Thumbnail")); ?>
</a>
<?php } ?>
</div>
<div class="review-body">
<h6>
<?php the_time('F Y') ?>
<?php //echo get_the_term_list( $post->ID, 'accolades', '| '); ?>
</h6>
<h3><a href="<?php echo $mainurl; ?>">
<?php the_title() ?>
</a></h3>
<?php if ($sh = $meta['subheader']){ echo "<h4> $sh </h4>"; } ?> <!-- line 58 -->
<div class="review-content">
<?php the_content(); ?>
<?php edit_post_link( 'Edit', '<div class="edit-link">', '</div>' ); ?>
</div>
<?php if (isset($meta['link_title'])): ?>
<ul class="url-list">
<li> <a href="<?php echo $meta['the_link']; ?>" class="note-cat">
<?php echo $meta['link_title']; ?></a> </li>
</ul>
<?php endif; //urls ?>
</div>
</div>
<?php endwhile; ?>
<p><a class="to-top" title="Top of the page" href="#masthead">Top↑</a></p>
<?php endif; //End if for Accolades ?>
</div>
<?php get_footer(); ?>
我认为你的问题是 $meta
不是一个数组,因为 get_post_meta
当前 return 是单个值,因为第三个参数设置为 true。
Wordpress文档中第三个参数的说明:
$single
(bool) (Optional) Whether to return a single value.
Default value: false
您目前正在使用:
$meta = get_post_meta($post->ID, '_urls', true);
第三个参数设置为true。所以它将 return 一个单一的值(不是一个数组)。所以你可以将它设置为 false(或者删除第三个参数,因为默认值为 false)到 return 一个数组。
您可以在此处找到有关 get_post_meta
函数的更多信息:
https://developer.wordpress.org/reference/functions/get_post_meta/
关于 'Warning: Illegal string offset' 错误的信息: