WordPress不顯示換行

在這裏插入圖片描述

作爲一名開發人員,在文章中插入代碼簡直太正常不過了,當我裝上Snape後,發現我在插入代碼的時候,古騰堡編輯器顯示是這樣的:

public class Test{
  publict static void main(String args[][]){
  }
}

發佈後,文章裏顯示確實這樣的, 這糟糕的排版讓我極難接受 :

public class Test{public static void main(String args[][]){}}

原文地址


環境

  • PHP_v7.2.24
  • WordPress_v5.3.2 點擊下載
  • Snape主題_v1.1.1

分析

當我搜索解決辦法時發現,發現這種吃掉\n的現象有很多人遇到過,網上亦有很多解決辦法,大致分這幾種:

  • 在每行後面手動加<br>(這也太麻煩了)
public class Test{<br>
  publict static void main(String args[][]){<br>
  }<br>
}<br>
  • 安裝 TinyMCE Advanced 插件 下載地址(現在國內訪問wordpress官網幾乎全是429,我把下載好的放抗揍雲上)

安裝後在設置中找到選項’ stop removing the p and br tags when saving and show them in the HTML editor ‘,打上勾。

  • 從hook中移除wpautop

在functions.php末尾添加這兩行:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

**但是這些方法對我這種情況都不管用!(欲哭無淚) **

把posts表中的一篇文章取出來放txt裏,格式正常,說明posts中是存有換行的,說明古騰堡不背鍋。

當我啓用TwentySeventeen主題,並沒有出現換行不顯示的現象。分別啓動Snape和TwentySeventeen,對比瀏覽器加載時的Source終於發現了端倪。
在這裏插入圖片描述
在這裏插入圖片描述
顯然,啓用Snape時,返回的是經過壓縮的html—-果然在functions.php裏發現了壓縮html的動作鉤子。

/**
 * Compress html
 */
function wp_compress_html(){
    function wp_compress_html_main ($buffer){
        $initial=strlen($buffer);
        $buffer=explode("<!--wp-compress-html-->", $buffer);
        $count=count ($buffer);
        for ($i = 0; $i <= $count; $i++){
            if (stristr($buffer[$i], '<!--wp-compress-html no compression-->')) {
                $buffer[$i]=(str_replace("<!--wp-compress-html no compression-->", " ", $buffer[$i]));
            } else {
                $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
                $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
                $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
                $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
                while (stristr($buffer[$i], '  ')) {
                    $buffer[$i]=(str_replace("  ", " ", $buffer[$i]));
                }
            }
            $buffer_out.=$buffer[$i];
        }
        $final=strlen($buffer_out);   
        $savings=($initial-$final)/$initial*100;   
        $savings=round($savings, 2); 
    return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');

流程

Snape主題出現不顯示換行的情況,直接去functions.php裏找上面這些代碼,刪掉它。

歡迎留言指正。

Reference

原文地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章