如果您希望在后台文章编辑框中使用 WYSIWYG 编辑器,可以考虑以下替代方案:
-
自定义文章类型:创建一个自定义文章类型,并在其编辑器中使用 WYSIWYG 编辑器。这样,您可以在后台创建具有样式的文章,并在前台使用自定义模板来显示它们。
-
自定义文章元框:创建一个自定义文章元框(Meta Box),在其中使用 WYSIWYG 编辑器。这样,您可以在文章编辑页面的侧边栏或底部添加一个具有 WYSIWYG 编辑器的自定义字段,以便在后台为每篇文章输入自定义内容。
方法一: 在functions.php中添加以下代码
add_action('add_meta_boxes', 'add_custom_meta_box');
function add_custom_meta_box() {
add_meta_box('custom_text_meta_box', 'Custom Text', 'render_custom_text_meta_box', 'post', 'normal', 'high');
}
// 渲染自定义文章元框
function render_custom_text_meta_box($post) {
$content = get_post_meta($post->ID, 'custom_text', true);
$editor_id = 'custom_text_editor';
wp_editor($content, $editor_id, array(
'textarea_name' => 'custom_text',
'textarea_rows' => 10,
'wpautop' => true
));
}
// 保存自定义文章元框的值
add_action('save_post', 'save_custom_text_meta_box');
function save_custom_text_meta_box($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (isset($_POST['custom_text'])) {
$custom_text = wp_kses_post($_POST['custom_text']);
update_post_meta($post_id, 'custom_text', $custom_text);
}
}
方法二:安装Advanced Custom Fields插件