[英]How to prevent and fix “Undefined offset: 0 in” error message
I have a function that is run in a WordPress plugin and when wp_debug is activated I receive this error message. 我有一个在WordPress插件中运行的函数,当wp_debug被激活时,我收到此错误消息。
" Undefined offset: 0 in /…/wp-content/plugins/advanced-widget-pack/lib/advanced_widget_pack.class.php on line 568 " “ 未定义的偏移:在568行的/.../wp-content/plugins/advanced-widget-pack/lib/advanced_widget_pack.class.php中为0 ”
Here is the function that is used in the plugin: 这是插件中使用的函数:
/**
* Retrieves the image for a post
*
* Uses the post_thumbnails if available or
* searches through the post and retrieves the first found image for use as thumbnails
*/
function featured_image_thumb($size = 50) {
global $post;
// If a featured image has been set, use the featured-thumbnail size that was set above with the class of 'thumb'
if(has_post_thumbnail() ) {
echo '<a href="'.get_permalink().'" title="'.get_the_title().'" >';
the_post_thumbnail(array($size,$size),array('class' => 'thumb'));
echo '</a>';
}
// If a featured image is not set, get the first image in the post content
else {
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
// Define a default fallback image in case a featured image is not set and there are no images in the post content
if(empty($first_img)){
$first_img = WP_PLUGIN_URL.'/advanced-widget-pack/images/nothumb.png';
}
// Generate the HTML code to display the image and resize the image with timthumb.php
return '<a title="'.get_the_title().'" href="'.get_permalink().'"><img class="thumb" src="'.WP_PLUGIN_URL.'/advanced-widget-pack/timthumb.php?src=' . $first_img .'&w='.$size.'&h='.$size.'" alt="" /></a>';
}
}
Line 568 or the place that the error is occurring is this section of the code: 第568行或发生错误的地方是代码的这一部分:
$first_img = $matches[1][0];
I am not sure how to prevent or fix this error. 我不知道如何防止或修复此错误。
Any help or advise would be greatly appreciated. 任何帮助或建议将不胜感激。 Thanks
谢谢
Undefined offset $X[Y] means that the array X does not have a key Y. In your case $matches[1] does not have a first element. 未定义的偏移量$ X [Y]表示数组X没有键Y.在您的情况下,$ matches [1]没有第一个元素。
I think you have to use a conditional : 我认为你必须使用一个条件:
if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
$first_img = $matches[1][0];
}
In my case an embracing with a conditional did it for me: 在我的情况下,有条件的拥抱为我做了:
if( $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); )
All the Notices are gone! 所有通知都没了! :)
:)
Means that that first index of the array is not defined prob because the preg_match above it failed to find anything. 意味着数组的第一个索引未定义,因为它上面的preg_match无法找到任何内容。 You shouldnt have to worry about it really but putting the following after ob_end_clean should take care of it
你不应该真的担心它,但在ob_end_clean之后放下以下内容应该照顾它
$matches[1][0] = '';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.