wordpress 外觀Manaslu themes 修正BUS,Warning: Undefined variable $excerpt_length in ../wp-content/themes/manaslu/inc/custom-functions.php on line 505

這個錯誤是由於 $excerpt_length 變數未定義而引起的, PHP 中,使用未定義的變數會導致警告,

解決此問題

檢查 $excerpt_length 的用途,並在使用之前進行適當的定義或檢查。

找到 custom-functions.php 中的第 505 行,然後確保 $excerpt_length 已正確定義

修正寫法:

function manaslu_excerpt_more( $more ) {
  
    if ( is_admin() && !wp_doing_ajax() ) {
        // 如果 $excerpt_length 未定義,給予一個預設值
        $excerpt_length = isset($excerpt_length) ? $excerpt_length : 55; // 預設摘要長度
        return $excerpt_length;
    }

    $flag_apply_excerpt_read_more = apply_filters( 'manaslu_filter_excerpt_read_more', true );
    if ( true !== $flag_apply_excerpt_read_more ) {
        return $more;
    }

    $output = $more;
    $read_more_text = esc_html__( ' ', 'manaslu' );
    if ( ! empty( $read_more_text ) ) {
        $output = ' <a href="' . esc_url( get_permalink() ) . '" class="read-more">' . esc_html( $read_more_text ) . '<i class="ion-ios-arrow-right read-more-right"></i>' . '</a>';
        $output = apply_filters( 'manaslu_filter_read_more_link', $output );
    }

    return $output;
}

原本寫法:

   function manaslu_excerpt_more( $more ) {
        if ( is_admin() && !wp_doing_ajax()) {
            return $excerpt_length;
        }
        $flag_apply_excerpt_read_more = apply_filters( 'manaslu_filter_excerpt_read_more', true );
        if ( true !== $flag_apply_excerpt_read_more ) {
            return $more;
        }

        $output = $more;
        $read_more_text = esc_html__(' ','manaslu');
        if ( ! empty( $read_more_text ) ) {
            $output = ' <a href="'. esc_url( get_permalink() ) . '" class="read-more">' . esc_html( $read_more_text ) . '<i class="ion-ios-arrow-right read-more-right"></i>' . '</a>';
            $output = apply_filters( 'manaslu_filter_read_more_link' , $output );
        }
        return $output;

    }

以上方法能有效避免 $excerpt_length 未定義的錯誤,並使程式碼更穩定。依具體需求,可以選擇直接在函式內定義預設值、從全域配置中讀取,或使用 WordPress 過濾器來優化程式的結構。

透過這些最佳實踐,不僅能修正問題,還能提升整體開發效率與代碼質量。

+ There are no comments

Add yours

發表迴響