11

我的wordpress代码有一个小问题我需要在我的页面中显示一个wordpress wp_editor,它有一个值数组。值的定义如下

    $fields[] = array(
        'name' => __('Class', 'my-theme'),
        'desc' => __('', 'my-theme'),
        'id' => 'class'.$n,
        'std' => ( ( isset($class_text[$n]['class']) ) ? $class_text[$n]['class'] : '' ),
        'type' => 'text');

当我像上面的数组一样定义我的 wp_editor 时,它不会显示我想要的位置。而是在所有页面中的任何内容之前显示在顶部的所有编辑器。

我为编辑器尝试了以下一组数组:

    $fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => wp_editor( '', 'sectioncontent'.$n ));

附上我的问题的图片:

在此处输入图像描述

4

2 回答 2

4

原因

默认情况下wp_editor打印 textarea 这就是为什么你不能将它分配给任何变量或数组的原因。

解决方案

您可以使用php 的输出缓冲来获取变量中的打印数据,如下所示:

ob_start(); // Start output buffer

// Print the editor
wp_editor( '', 'sectioncontent'.$n );

// Store the printed data in $editor variable
$editor = ob_get_clean();

// And then you can assign that wp_editor to your array.

$fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => $editor); // <-- HERE
于 2015-10-29T05:21:48.610 回答
1

在我看来,您正在使用Redux Framework来设置您的主题/插件选项页面 - 如果您正在寻找添加默认的 Wordpress WYSIWYG(所见即所得 - 来自后端编辑帖子页面的相同编辑器) 编辑器,你需要使用类型:'editor'。

这可能会令人困惑 -wp_editor() 如果您从头开始设置此选项页面,您正在使用的功能是正确的起点,但您需要做很多工作才能让它显示在您想要的位置和方式。Redux 等人通过为您生成编辑器使这对您来说更容易一些,因此您根本不需要使用 wp_editor 函数,您只需告诉 Redux 您想要一个名为“我的内容”的编辑器字段作为字段之一这页纸。

编辑器字段的文档在这里:https ://docs.reduxframework.com/core/fields/editor/

如果我对您使用的是 redux 是正确的,那么替换您所拥有的正确代码是:

 $fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => 'editor');

解释这个字段数组的其他部分:

  • “名称”将显示在该字段的标签中。在这种情况下,您使用 wordpress ( __()) 中的本地化功能从“我的主题”域中的本地词典中获取短语。
  • 'id' 是您将用于检索已输入此字段的内容。它还将影响分配给选项页面中 HTML 元素的 ID 属性。
  • 'std' 是字段的默认值,它将是第一次显示选项页面时的字段值,在用户设置任何选项之前

在上面链接的编辑器文档页面上,您将看到可以定义的各种其他选项的详细信息,例如是否显示媒体上传按钮,以及是否通过 wpautop 运行输入以将编辑器中的换行符替换为<p>标签(默认情况下这两个都是真的)。

于 2015-11-02T06:11:25.290 回答