如何在wordpress页面中创建不同的可编辑部分?(How do I create different editable sections within a WordPress page?)

编程入门 行业动态 更新时间:2024-10-28 12:23:03
如何在wordpress页面中创建不同的可编辑部分?(How do I create different editable sections within a WordPress page?)

我一直在构建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中?

我搜索了几个论坛,许多人建议使用小部件添加内容,有什么方法可以在不使用小部件的情况下完成?

任何帮助将很乐意欣赏。

I have been building my first theme on WordPress and have run into problem while adding content into different sections.

My HTML is somewhat like this,

<div id="maintext"> <-- Text --> </div> <div id="products"> <-- Text and Images --> </div> <div id="about_company"> <-- Text boxes --> </div>

How do I make sure the content added via the WordPress editor falls under the respective divs ? For the "maintext" div I'll load the content from the page itself but how do I add content to the other 2 divs dynamically ?

I searched on a couple of forums and many suggested to add content using widgets, is there any way it can be done without using widgets ?

Any help will be gladly appreciated.

最满意答案

不幸的是,在单个页面中添加多个可编辑字段对于使用Wordpress本身来说并不是特别容易。

许多我认识的WP开发人员(包括我自己)都依赖高级自定义字段插件来获取更多内容字段。

做到这一点的步骤:

1)安装ACF插头。 2)在ACF的设置区域创建一些新的字段。 3)分配新字段以显示特定页面或一组页面。 4)更新给定页面的页面模板,以显示新的字段。

例如,您可以创建一组标准wysiwyg字段,然后将其分配到“概览”页面。 我们来调用这些字段:main_text,products_info和about_company。 一旦这些字段被创建并分配给页面,当您编辑该页面时,其他字段将可用于编辑。

对于访问者可以看到的这些新字段,必须将它们添加到用于概览页面的页面模板中。 代码可能是这样的:

<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>

这里有很多很好的例子 。 如果你感觉真的很有野心,而不是安装插件,你甚至可以直接在你的主题中包含ACF。

Unfortunately adding multiple editable fields in a single page is not particularly easy using WordPress by itself.

Many WP devs I know (myself included) rely on the Advanced Custom Fields Plugin for additional content fields.

The steps to make this happen:

1) Install the ACF the plug. 2) In the settings area for ACF create some new fields. 3) Assign the new fields to appear for a specific page or set of pages. 4) Update your page-template for the given page(s) so that the new fields are displayed.

For instance you might create a set of standard wysiwyg fields and then assign them to the 'overview' page. Let's call these fields: main_text, products_info and about_company. Once the fields have been created and assigned to a page, when you edit that page the additional fields will be available to edit.

For these new fields to be seen by visitors, they must be added to the page-template you use for your overview page. The code could be something like this:

<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>

There are lots of good examples here. If you are feeling really ambitious, rather than install the plugin you could even include ACF directly in your theme.

更多推荐

content,WordPress,内容,添加,电脑培训,计算机培训,IT培训"/> <meta name="descr

本文发布于:2023-08-02 08:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1373227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   页面   如何在   wordpress   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!