如何修复post-template.php中未定义的偏移量错误


How to fix undefined offset error in post-template.php

我正试图帮助一家小企业使用Wordpress网站,但无法弄清楚他们的网站为什么会生成此错误。以下是详细信息:

错误是:未定义的偏移量:在第278行的/home/sojour15/public_html/wp-includes/post-template.php中为-1

这是post-template.php的代码,从第275行开始,第278行是"$content=$pages[$page-1];"

if ( $page > count( $pages ) ) // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist
$content = $pages[$page - 1];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
    $content = explode( $matches[0], $content, 2 );
    if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
        $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
    $has_teaser = true;
} else {
    $content = array( $content );
}

我读过一些关于未定义偏移错误的文章,知道这意味着代码引用了数组中不存在的东西,但我不是PHP程序员,只是试图帮助小企业的人,我不知道如何解决这个问题。我尝试了一个我在某个地方找到的破解方法——只需在278行前面加一个"@"。奇怪的是,这次黑客攻击持续了大约一周。现在它已经不起作用了——无论如何,最好正确地修复代码。任何指导都是非常受欢迎的。谢谢这里还有一个链接,指向发生这种情况的页面之一:https://www.sojournacupuncture.com/treatments-and-services/

$page可能具有0。因此,数组中的$pages[0-1],-1索引不存在。

您可以检查$page是否为空或为0,那么它不应该执行其余的代码。希望这对你有用。

if ( $page > count( $pages ) ) // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist
// A check on $page
// If it is not empty, then it should execute the rest
if (!empty($page)) {
    $content = $pages[$page - 1];
    if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
        $content = explode( $matches[0], $content, 2 );
        if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
            $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
        $has_teaser = true;
    } else {
        $content = array( $content );
    }
}

试试这个代码。

if ( $page > count( $pages ) ) // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist
$content = ctype_digit($page) && $page > 1 ? $pages[$page - 1] : $pages[0];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
    $content = explode( $matches[0], $content, 2 );
    if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
        $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
    $has_teaser = true;
} else {
    $content = array( $content );
}

如果它不好,并且你的网站运行良好,那么。。您可以按照下面链接中的说明隐藏警告和通知

隐藏WORDPRESS警告和通知