问题描述

我一直在 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。