問題描述
我一直在 WordPress 上構建我的第一個主題,並在將內容新增到不同的部分時遇到問題。
我的 HTML 有點像這樣,
<div id="maintext">
<-- Text -->
</div>
<div id="products">
<-- Text and Images -->
</div>
<div id="about_company">
<-- Text boxes -->
</div>
如何確保透過 wordpress 編輯器新增的內容屬於相應的 div?對於”maintext” div,我將從頁面載入內容,但如何動態地將內容新增到其他 2 個 div?
我在幾個論壇上搜尋,許多建議使用小工具新增內容,是否有任何方式可以在不使用小工具的情況下完成?
任何幫助將高興的讚賞。
最佳解決方法
不幸的是,在單個頁面中新增多個可編輯欄位不是特別容易使用 Wordpress 本身。
我知道的許多 WP 開發者 (包括我自己) 依賴於 Advanced Custom Fields Plugin 來獲得更多的內容欄位。
使這種情況發生的步驟:
1) 安裝 ACF 插頭。 2) 在 ACF 的設定區域中建立一些新的欄位。 3) 為特定頁面或一組頁面分配新的欄位。 4) 更新給定頁面的 page-template,以便顯示新的欄位。
例如,您可以建立一組標準 wisiwig 欄位,然後將其分配給’overview’ 頁面。讓我們稱這些欄位:main_text,products_info 和 about_company 。一旦欄位已建立並分配給頁面,當您編輯該頁面時,其他欄位將可用於編輯。
對於訪問者可以看到的這些新欄位,必須將其新增到您用於概述頁面的 page-template 中。程式碼可能是這樣的:
<div id="maintext">
<!-- Text -->
<?php if(get_field('main_text')){ //if the field is not empty
echo '<p>' . get_field('main_text') . '</p>'; //display it
} ?>
</div>
<div id="products">
<!-- Text and Images -->
<?php if(get_field('products_info')){ //if the field is not empty
echo '<p>' . get_field('products_info') . '</p>'; //display it
} ?>
</div>
<div id="about_company">
<!-- Text boxes -->
<?php if(get_field('about_company')){ //if the field is not empty
echo '<p>' . get_field('about_company') . '</p>'; //display it
} ?>
</div>
有很多很好的 examples here 。如果你真的有野心,而不是安裝外掛,甚至可以包括 ACF directly in your theme 。
次佳解決方法
<div id="maintext">
<?php the_content(); ?>
</div>
<div id="products">
<?php // echo wp function to get product data; ?>
</div>
<div id="about_company">
<?php // echo wp function to get about companydata; ?>
</div>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。