用 Elementor 構(gòu)建你的頁面時(shí),有時(shí)會遇到這樣一個(gè)問題:編輯器加載失敗、預(yù)覽頁面空白、提示找不到內(nèi)容。這種情況很可能是主題中的 PHP 模板缺少了核心函數(shù) the_content()
的調(diào)用。
本篇文章就帶你了解這個(gè)函數(shù)的作用,以及在自定義模板中怎樣正確添加它,避免 Elementor 出現(xiàn)報(bào)錯(cuò)或內(nèi)容無法渲染的情況。

the_content() 是干什么用的?
the_content()
是 WordPress 中最基本也最關(guān)鍵的函數(shù)之一。它的作用是將文章或頁面的主體內(nèi)容輸出在模板中。比如后臺編輯器中寫的段落、圖片、短代碼等,都會通過它在前臺展示出來。
如果在自定義模板里省略了這個(gè)函數(shù),WordPress 前臺雖然加載了頁面,但正文內(nèi)容是不會顯示的。這對于依賴 the_content()
輸出內(nèi)容的插件(例如 Elementor)來說,會造成嚴(yán)重影響。
出現(xiàn)問題的常見場景
- 自定義了 page.php 或 single.php 模板,卻沒有加
the_content()
- 用 Elementor 編輯某個(gè)頁面時(shí)加載失敗
- 頁面預(yù)覽時(shí)內(nèi)容區(qū)域是空的
- 頁面生成的 HTML 中,主體部分是
<main></main>
,中間什么都沒有
![圖片[2]-在自定義 PHP 模板中正確添加 the_content() 消除 Elementor 報(bào)錯(cuò)](http://gqxi.cn/wp-content/uploads/2025/06/20250612170521994-image.png)
這些問題大多數(shù)可以追溯到一個(gè)原因:模板結(jié)構(gòu)不完整。
正確添加 the_content() 的位置
在自定義模板文件中,the_content()
應(yīng)該出現(xiàn)在主體容器的內(nèi)部。推薦結(jié)構(gòu)如下:
<main id="main" class="site-main">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_content();
}
}
?>
</main>
![圖片[3]-在自定義 PHP 模板中正確添加 the_content() 消除 Elementor 報(bào)錯(cuò)](http://gqxi.cn/wp-content/uploads/2025/06/20250612165030938-image.png)
這個(gè)結(jié)構(gòu)的核心點(diǎn)在于:
- 使用了 WordPress 的
have_posts()
和the_post()
循環(huán) the_content()
被包含在主循環(huán)內(nèi)- 放在
<main>
或<div class="content-area">
這樣的容器中,便于主題或插件識別
示例:自定義頁面模板加入 the_content()
假設(shè)你有一個(gè)自定義頁面模板文件 template-custom.php
,應(yīng)該這樣處理:
<?php
/**
* Template Name: Custom Page
*/
get_header(); ?>
<main id="main" class="site-main">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_content();
}
}
?>
</main>
<?php get_footer(); ?>
保存后重新訪問頁面或進(jìn)入 Elementor,即可恢復(fù)內(nèi)容加載。
小提示:不要替代性使用 get_the_content()
有些人可能會誤用 get_the_content()
,但這個(gè)函數(shù)只是返回原始內(nèi)容字符串,不會自動處理短代碼、嵌套區(qū)塊等,也不會被 Elementor 等插件識別。要顯示渲染后的完整內(nèi)容,用 the_content()
。
如何檢查主題文件是否遺漏 the_content()
- 打開你當(dāng)前使用主題的
page.php
或single.php
![圖片[4]-在自定義 PHP 模板中正確添加 the_content() 消除 Elementor 報(bào)錯(cuò)](http://gqxi.cn/wp-content/uploads/2025/06/20250612170839764-image.png)
- 搜索
the_content()
是否存在 - 如果是用子主題,記得查看是否覆蓋了父主題模板
- Elementor 報(bào)錯(cuò)時(shí),配合 Debug 模式開啟(wp-config.php 中設(shè)置
WP_DEBUG
為 true)
![圖片[5]-在自定義 PHP 模板中正確添加 the_content() 消除 Elementor 報(bào)錯(cuò)](http://gqxi.cn/wp-content/uploads/2025/06/20250612171237273-image.png)
結(jié)論
在 WordPress 自定義開發(fā)中,很多問題看似復(fù)雜,其實(shí)源頭非常簡單。如果你用 Elementor 構(gòu)建頁面,建議始終確認(rèn)主題或自定義模板中包含了這個(gè)函數(shù)。這樣能保證內(nèi)容正常顯示,也會避免了很多意料之外的排版和功能錯(cuò)誤。
最近更新
聯(lián)系我們 | |
---|---|
教程看不懂?聯(lián)系我們?yōu)槟赓M(fèi)解答!免費(fèi)助力個(gè)人,小企站點(diǎn)! |
![]() 客服微信
|
① 電話:020-2206-9892 | |
② QQ咨詢:1025174874 | |
③ 郵件:info@361sale.com | |
④ 工作時(shí)間:周一至周五,9:30-18:30,節(jié)假日休息 |
暫無評論內(nèi)容