問題描述
我一直在 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。