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

查看:30
本文介绍了如何在 WordPress 页面中创建不同的可编辑部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 WordPress 上构建我的第一个主题,但在将内容添加到不同部分时遇到了问题.

我的 HTML 有点像这样,

<-- 文本-->

<div id="产品"><-- 文本和图像-->

<div id="about_company"><-- 文本框-->

如何确保通过 WordPress 编辑器添加的内容属于相应的 div ?对于maintext"div,我将从页面本身加载内容,但如何将内容动态添加到其他 2 个 div?

我在几个论坛上搜索过,很多人建议使用小部件添加内容,有什么方法可以不使用小部件来完成吗?

任何帮助将不胜感激.

解决方案

不幸的是,单独使用 WordPress 在单个页面中添加多个可编辑字段并不是特别容易.

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

实现这一目标的步骤:

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

例如,您可以创建一组标准所见即所得字段,然后将它们分配到概览"页面.让我们称这些字段为:main_text、products_info 和 about_company.创建字段并将其分配给页面后,当您编辑该页面时,其他字段将可供编辑.

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

<!-- 文本--><?php if(get_field('main_text')){//如果字段不为空回声'<p>'.get_field('main_text') .'</p>';//显示它?>

<div id="产品"><!-- 文本和图像--><?php if(get_field('products_info')){//如果字段不为空回声'<p>'.get_field('products_info') .'</p>';//显示它?>

<div id="about_company"><!-- 文本框--><?php if(get_field('about_company')){//如果字段不为空回声'<p>'.get_field('about_company') .'</p>';//显示它?>

这里有很多很好的示例.如果你真的很有野心,而不是安装插件,你甚至可以包括 ACF 直接在您的主题中.

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.

解决方案

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.

这篇关于如何在 WordPress 页面中创建不同的可编辑部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆