我正在使用这段代码在WordPress中嵌入要点。它工作得很好,但我的debug.log文件中一直有一个错误:PHP注意:未定义的偏移量:2在/functions.php中的第333行中。
esc_attr($matches[2])**)** (第333行为)
wp_embed_register_handler( 'gist', '/https?:\/\/gist\.github\.com\/([a-z0-9]+)(\?file=.*)?/i', 'embed_handler_gist' );
function embed_handler_gist( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<script src="https://gist.github.com/%1$s.js%2$s"></script>',
esc_attr($matches[1]),
esc_attr($matches[2])
);
return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr );
}发布于 2015-10-28 01:59:25
这个错误仅仅意味着$matches[2]不存在。可以通过在尝试访问变量之前检查是否存在此错误来避免此错误。
if( isset($matches[2]) )
esc_attr($matches[2]);或者,如果您希望分配一个默认值:
$value = isset($matches[2]) ? $matches[2] : false;isset()
发布于 2015-10-28 00:29:07
当我试图从mysql数据库中提取一个媒体列表时,我遇到了一个偏移问题。当我在数据库中只有一个条目时,它给了我一个偏移错误。检查是否同时返回两个变量。如果你只得到一个,也许你可以用一个大号($array)或计数($array)来编程一个错误检查。只是一个想法:)
https://stackoverflow.com/questions/33380683
复制相似问题