本文介绍: 在制作wordpress模板时,有时会用到同一个文章需要分开录入内容,分别调用的情况,这个时候就需要给文章,再添加一个录入额外内容的编辑器。将下面的代码添加到functions.php中,就可以实现。添加完了后,在录入文章时,就可以显示出来。在此编辑器中录入内容,在需要的地方调用出来就可以。

在制作wordpress模板时,有时会用到同一个文章需要分开录入内容,分别调用的情况,这个时候就需要给文章,再添加一个录入额外内容的编辑器。将下面的代码添加到functions.php中,就可以实现。

function wodepress_post_editor_meta_box() {    
   add_meta_box ( 
      'wpkj-post-editor', 
      __('文章顶部内容', 'textdomain') , 
      'wodepress_post_editor', 
      'post' // 需要显示编辑框的文章类型,与下文的两处 $_POST['post'] 对应
   );
 
}
add_action('admin_init', 'wodepress_post_editor_meta_box');
 
//Displaying the meta box
function wodepress_post_editor($post) {          
    $content = get_post_meta($post->ID, 'wodepress_post_editor', true);
 
    //This function adds the WYSIWYG Editor 
    wp_editor ( 
        $content , 
        'wodepress_post_editor', 
        array ( "media_buttons" => true ) 
    );
 
}
 
//This function saves the data you put in the meta box
function wodepress_post_editor_save_postdata($post_id) {
 
    if( isset( $_POST['wodepress_post_editor_nonce'] ) && isset( $_POST['post'] ) ) {
 
        //Not save if the user hasn't submitted changes
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        } 
 
        // Verifying whether input is coming from the proper form
        if ( ! wp_verify_nonce ( $_POST['wodepress_post_editor_nonce'] ) ) {
            return;
        } 
 
        // Making sure the user has permission
        if( 'post' == $_POST['post'] ) {
            if( ! current_user_can( 'edit_post', $post_id ) ) {
                return;
            }
        } 
    }
 
    $content = get_post_meta($post_id, 'wodepress_post_editor', true);
    // 如果编辑器中有内容或者之前有数据才保存
    if( $content || !empty( $_POST['wodepress_post_editor'] ) ) {
 
        $data = $_POST['wodepress_post_editor'];
        update_post_meta($post_id, 'wodepress_post_editor', $data);
 
    }
}
add_action('save_post', 'wodepress_post_editor_save_postdata');

添加完了后,在录入文章时,就可以显示出来。在此编辑器中录入内容,在需要的地方调用出来就可以。

<?php
global $post;
$content = get_post_meta( $post->ID, 'wodepress_post_editor', true ); // 获取字段内容
if( $content ) { // 如果有内容
    echo $content;  // 输出内容
}
?>

 原文链接 https://www.zhanyes.com/code/6048.html

原文地址:https://blog.csdn.net/jianzhanyes/article/details/135565990

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_57222.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注