如何在wordpress中创建多个内容块?

如何在wordpress中创建多个内容块?,wordpress,Wordpress,提前感谢您解释在管理面板的Wordpress post editor中创建多个内容块的想法。我试着在询问之前搜索类似的线索,但找不到任何答案。 我需要的是在创建默认内容字段的同时创建一个附加内容字段。请问我需要实现哪些功能?我在wordpress插件库中发现了一个插件“多内容块”,但我相信这个简单的任务需要更少的代码。我希望我已经很好地解释了我需要什么。再次感谢 首先,在Wordpress编辑页面中添加内容编辑器要比听起来困难得多,因此如果您不熟悉保存/更新周期和元数据库,我建议您使用插件。我喜

提前感谢您解释在管理面板的Wordpress post editor中创建多个内容块的想法。我试着在询问之前搜索类似的线索,但找不到任何答案。
我需要的是在创建默认内容字段的同时创建一个附加内容字段。请问我需要实现哪些功能?我在wordpress插件库中发现了一个插件“多内容块”,但我相信这个简单的任务需要更少的代码。我希望我已经很好地解释了我需要什么。再次感谢

首先,在Wordpress编辑页面中添加内容编辑器要比听起来困难得多,因此如果您不熟悉保存/更新周期和元数据库,我建议您使用插件。我喜欢“高级自定义字段”,但我相信“多内容块”也不错

无论如何,我在这里概述了一个通用的自定义元盒解决方案。现在我们开始:

wp\u editor()
函数是我们用来创建编辑器实例的函数

但是,我会将其称为元框。

下面是一些示例代码,用于创建包含内容编辑器的元框

此插件将内容编辑器的值存储在名为
\u hurtigtech\u extra\u content
的自定义字段中,该字段在更新帖子/页面时保存

您可以将此插件放入plugins文件夹
/wp content/plugins/
,然后在那里玩。如果你在这方面需要帮助,请随时留下评论,我知道这是很多代码,所以插件可能是最好的,但如果你有信心,这也是一个很好的基线

<?php
/**
 * Plugin Name: Extra Metabox Content Editor
 */

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function hurtigtech_add_custom_box() {

    $screens = array( 'post', 'page' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            'hrutigtech_extra_content_section',
            __( 'My Post Extra Content', 'hurtigtech_translations' ),
            'hurtigtech_inner_custom_box',
            $screen
        );
    }
}
add_action( 'add_meta_boxes', 'hurtigtech_add_custom_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function hurtigtech_inner_custom_box( $post ) {

  // Add an nonce field so we can check for it later.
  wp_nonce_field( 'hurtigtech_inner_custom_box', 'hurtigtech_inner_custom_box_nonce' );

  /*
   * Use get_post_meta() to retrieve an existing value
   * from the database and use the value for the form.
   */
  $value = get_post_meta( $post->ID, '_hurtigtech_extra_content', true );
  echo '<br />';
  wp_editor( $value, "hurtigtech_extra_content_editor");
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function hurtigtech_save_postdata( $post_id ) {

  /*
   * We need to verify this came from the our screen and with proper authorization,
   * because save_post can be triggered at other times.
   */

  // Check if our nonce is set.
  if ( ! isset( $_POST['hurtigtech_inner_custom_box_nonce'] ) )
    return $post_id;

  $nonce = $_POST['hurtigtech_inner_custom_box_nonce'];

  // Verify that the nonce is valid.
  if ( ! wp_verify_nonce( $nonce, 'hurtigtech_inner_custom_box' ) )
      return $post_id;

  // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return $post_id;

  // Check the user's permissions.
  if ( 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;

  } else {

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  /* OK, its safe for us to save the data now. */

  // Sanitize user input.
  $mydata = sanitize_text_field( $_POST['hurtigtech_extra_content_editor'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, '_hurtigtech_extra_content', $mydata );


}
add_action( 'save_post', 'hurtigtech_save_postdata' );

这是一个老生常谈的问题,但由于它是我在谷歌搜索中的第一个热门话题,我将添加我的方法-在公认的答案中存在一些主要问题,主要是:

  • 当文档明确说明
    wp\u editor()
    不安全时,使用元框
  • 使用
    sanitize\u text\u field()
    清理html(删除所有html!)
该文件可以放入主题文件夹或functions.php的底部,所有文章和页面都会有一个额外的编辑器

如果使用单独的文件,请记住将其包含在functions.php中:

include __DIR__ . '/my_extra_content.php';
显然,你应该搜索“我的内容”并替换为有意义的内容——“额外的内容”可能也有点太笼统了,所以想出一些更具异国情调的内容

<?php
//Use a class to avoid conflicts
class my_extra_content {
    /**
     * Called on instantiation, this is where we hook functions
     */
    function __construct() {
        /* Using add_meta_box seems like the correct way to do this, but since
         * we're inserting a TinyMCE editor we cannot (should not) - from codex:
         * ---
         * Once instantiated, the WYSIWYG editor cannot be moved around in the
         * DOM. What this means in practical terms, is that you cannot put it in
         * meta-boxes that can be dragged and placed elsewhere on the page.
         * Instead use 'edit_page_form' (for pages) or 'edit_form_advanced'
         * (for other post types).
         */
        add_action( 'edit_page_form', array($this, 'my_extra_content_custom_box') );
        add_action( 'edit_form_advanced', array($this, 'my_extra_content_custom_box') );

        /* This one saves the content */
        add_action( 'save_post', array($this, 'save_postdata' ));
    }

    /**
     * This actually outputs the tinyMCE box
     */
    function my_extra_content_custom_box( $post ) {
        /* Always use a nonce */
        wp_nonce_field( 'my_extra_content_custom_box', 'my_extra_content_custom_box_nonce' );        

        /* Get the content */
        $content = self::get_content($post);

        /* Insert the editor */
        wp_editor( $content, "my_extra_content");
    }

    /**
     * Saves the content
     */
    function save_postdata( $post_id ) {
        /* Check that nonce was sent */
        if ( ! isset( $_POST['my_extra_content_custom_box_nonce'] ) ) {
            return $post_id;
        }
        /* Check that nonce is valid */
        if ( ! wp_verify_nonce( $_POST['my_extra_content_custom_box_nonce'], 'my_extra_content_custom_box' ) ) {
            return $post_id;
        }
        /* Don't try to do anything on autosave (custom fields aren't included) */
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
        /* Check permissions */
        if ( 'page' === get_post_type( $post_id ) ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

        /* Sanitize content - we don't use sanitize_text_field() as it strips all
         * HTML, which is clearly not wanted with a wysiwyg - wp_kses_post()
         * should do what we want */
        $sane_content = wp_kses_post( $_POST['my_extra_content'] );

        /* Save content - notice the underscore in the meta name - it hides the
         * field from the "normal" custom field editor */
        update_post_meta( $post_id, '_my_extra_content_content', $sane_content );
    }

    /**
     *  Static function makes it easy to get the value wherever you need it.
     * - for example:
     * $my_extra_content = my_extra_content::get_content()
     */
    static function get_content($post_or_post_id = null) {
        /* First find the post id */
        $post_id = false;
        if ($post_or_post_id === null) {
            /* If nothing was passed, try to get it from global post object */
            global $post;
            $post_or_post_id = $post->ID;
        }
        if (is_a($post_or_post_id, 'WP_Post')) {
            /* If a post object was passed, or we're using the global $post */
            $post_id = $post_or_post_id->ID;
        } elseif (is_numeric($post_or_post_id)) {
            /* If a number (hopefully a post id) was passed */
            $post_id = intval($post_or_post_id);
        }

        /* Try to get the value */
        $value = get_post_meta($post_id, '_my_extra_content_content', true );

        /* If we didn't get a valid string return an empty one */
        if (!is_string($value)) {
            return '';
        }

        return $value;
    }
    /**
     * Static function to very easily output the content in a template
     * - for example:
     * my_extra_content::echo_content()
     */
    static function echo_content( $post_or_post_id = null ) {
        $output = self::get_content($post_or_post_id);

        /* do_shortcode makes sure we support shortcodes (if that is wanted) */
        // $output = do_shortcode($output);

        /* the_content filter will apply all normal filters (including
         * do_shortcode) to the content (not required!) */
        $output = apply_filters( 'the_content', $output);

        /* print it */
        echo $output;
    }
}

/* Instantiate the class - because of the static functions used to fetch the
 * content we won't need to ever use this variable, we just need __construct()
 * to be called, so our hooks are added */
$extra_content_throwaway_var = new my_extra_content();

100%使用ACF。太神奇了。非常简单,但一旦你开始使用它,它也非常强大。
<?php
//Use a class to avoid conflicts
class my_extra_content {
    /**
     * Called on instantiation, this is where we hook functions
     */
    function __construct() {
        /* Using add_meta_box seems like the correct way to do this, but since
         * we're inserting a TinyMCE editor we cannot (should not) - from codex:
         * ---
         * Once instantiated, the WYSIWYG editor cannot be moved around in the
         * DOM. What this means in practical terms, is that you cannot put it in
         * meta-boxes that can be dragged and placed elsewhere on the page.
         * Instead use 'edit_page_form' (for pages) or 'edit_form_advanced'
         * (for other post types).
         */
        add_action( 'edit_page_form', array($this, 'my_extra_content_custom_box') );
        add_action( 'edit_form_advanced', array($this, 'my_extra_content_custom_box') );

        /* This one saves the content */
        add_action( 'save_post', array($this, 'save_postdata' ));
    }

    /**
     * This actually outputs the tinyMCE box
     */
    function my_extra_content_custom_box( $post ) {
        /* Always use a nonce */
        wp_nonce_field( 'my_extra_content_custom_box', 'my_extra_content_custom_box_nonce' );        

        /* Get the content */
        $content = self::get_content($post);

        /* Insert the editor */
        wp_editor( $content, "my_extra_content");
    }

    /**
     * Saves the content
     */
    function save_postdata( $post_id ) {
        /* Check that nonce was sent */
        if ( ! isset( $_POST['my_extra_content_custom_box_nonce'] ) ) {
            return $post_id;
        }
        /* Check that nonce is valid */
        if ( ! wp_verify_nonce( $_POST['my_extra_content_custom_box_nonce'], 'my_extra_content_custom_box' ) ) {
            return $post_id;
        }
        /* Don't try to do anything on autosave (custom fields aren't included) */
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
        /* Check permissions */
        if ( 'page' === get_post_type( $post_id ) ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

        /* Sanitize content - we don't use sanitize_text_field() as it strips all
         * HTML, which is clearly not wanted with a wysiwyg - wp_kses_post()
         * should do what we want */
        $sane_content = wp_kses_post( $_POST['my_extra_content'] );

        /* Save content - notice the underscore in the meta name - it hides the
         * field from the "normal" custom field editor */
        update_post_meta( $post_id, '_my_extra_content_content', $sane_content );
    }

    /**
     *  Static function makes it easy to get the value wherever you need it.
     * - for example:
     * $my_extra_content = my_extra_content::get_content()
     */
    static function get_content($post_or_post_id = null) {
        /* First find the post id */
        $post_id = false;
        if ($post_or_post_id === null) {
            /* If nothing was passed, try to get it from global post object */
            global $post;
            $post_or_post_id = $post->ID;
        }
        if (is_a($post_or_post_id, 'WP_Post')) {
            /* If a post object was passed, or we're using the global $post */
            $post_id = $post_or_post_id->ID;
        } elseif (is_numeric($post_or_post_id)) {
            /* If a number (hopefully a post id) was passed */
            $post_id = intval($post_or_post_id);
        }

        /* Try to get the value */
        $value = get_post_meta($post_id, '_my_extra_content_content', true );

        /* If we didn't get a valid string return an empty one */
        if (!is_string($value)) {
            return '';
        }

        return $value;
    }
    /**
     * Static function to very easily output the content in a template
     * - for example:
     * my_extra_content::echo_content()
     */
    static function echo_content( $post_or_post_id = null ) {
        $output = self::get_content($post_or_post_id);

        /* do_shortcode makes sure we support shortcodes (if that is wanted) */
        // $output = do_shortcode($output);

        /* the_content filter will apply all normal filters (including
         * do_shortcode) to the content (not required!) */
        $output = apply_filters( 'the_content', $output);

        /* print it */
        echo $output;
    }
}

/* Instantiate the class - because of the static functions used to fetch the
 * content we won't need to ever use this variable, we just need __construct()
 * to be called, so our hooks are added */
$extra_content_throwaway_var = new my_extra_content();