programing

Wordpress의 게시 상자에 있는 게시물 편집 페이지에 필드를 추가하려면 어떻게 해야 합니까?

showcode 2023. 3. 11. 09:41
반응형

Wordpress의 게시 상자에 있는 게시물 편집 페이지에 필드를 추가하려면 어떻게 해야 합니까?

게시물 추가/편집 페이지의 블록 게시에서 체크박스를 새로 추가하고 싶습니다.어떻게 하는지 아는 사람 있어요?

나는 마침내 해결책을 찾았다.누군가에게 도움이 되었으면 좋겠어요.

add_action( 'post_submitbox_misc_actions', 'publish_in_frontpage' );
function publish_in_frontpage($post)
{
    $value = get_post_meta($post->ID, '_publish_in_frontpage', true);
    echo '<div class="misc-pub-section misc-pub-section-last">
         <span id="timestamp">'
         . '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
    .'</span></div>';
}

function save_postdata($postid)
{   
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $postid ) ) return false;
    if(empty($postid) || $_POST['post_type'] != 'article' ) return false;

    if($_POST['action'] == 'editpost'){
        delete_post_meta($postid, 'publish_in_frontpage');
    }

    add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}

rbncha의 코드는 처음부터 작동하지 않아 많은 수정이 필요했습니다.아래 코드는 제가 생각해낸 것입니다.모든 것을 자세히 설명하는 몇 가지각색의 설명을 덧붙였습니다.

다음 코드는 게시 블록에 체크박스를 추가하고(게시 유형을 쉽게 변경할 수 있음), 값을 데이터베이스에 저장/검색합니다.약간의 미세 조정으로 텍스트 필드나 원하는 항목을 쉽게 추가할 수 있습니다.

변경하셔야 합니다.my_테마 또는 플러그인의 고유 키로 이동합니다!

add_action( 'post_submitbox_misc_actions', 'my_featured_post_field' );
function my_featured_post_field()
{
    global $post;

    /* check if this is a post, if not then we won't add the custom field */
    /* change this post type to any type you want to add the custom field to */
    if (get_post_type($post) != 'post') return false;

    /* get the value corrent value of the custom field */
    $value = get_post_meta($post->ID, 'my_featured_post_field', true);
    ?>
        <div class="misc-pub-section">
            <?php //if there is a value (1), check the checkbox ?>
            <label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
        </div>
    <?php
}

add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
    /* check if this is an autosave */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;

    /* check if the user can edit this page */
    if ( !current_user_can( 'edit_page', $postid ) ) return false;

    /* check if there's a post id and check if this is a post */
    /* make sure this is the same post type as above */
    if(empty($postid) || $_POST['post_type'] != 'post' ) return false;

    /* if you are going to use text fields, then you should change the part below */
    /* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */

    /* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
    if(isset($_POST['my_featured_post_field'])){
        /* store the value in the database */
        add_post_meta($postid, 'my_featured_post_field', 1, true );
    }
    else{
        /* not marked? delete the value in the database */
        delete_post_meta($postid, 'my_featured_post_field');
    }
}

커스텀 필드의 상세한 것에 대하여는, http://codex.wordpress.org/Custom_Fields 를 참조해 주세요.

글쎄요! 게시 블록에 필드를 추가할 솔루션을 찾을 수 없습니다.임시 솔루션은 아래와 같이 간단한 코드를 추가하여 새로운 블록을 추가하였습니다.

add_action( 'admin_init', 'category_metabox');

//add new publish to frontpage box
add_meta_box( 
    'publish_in_frontpage',
    'Publish in Frontpage',
    'publish_in_frontpage_callback',
    'article',
    'side',
    'high'
);

function publish_in_frontpage_callback($post)
{
    $value = get_post_meta($post->ID, '_publish_in_frontpage', true);
    echo '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>';
}

add_action( 'save_post', 'save_postdata');

function save_postdata($postid)
{   
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $postid ) ) return false;
    if(empty($postid) || $_POST['post_type'] != 'article' ) return false;

    if($_POST['action'] == 'editpost'){
        delete_post_meta($postid, 'publish_in_frontpage');
    }

    add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}

워드프레스에 고급 사용자 지정 필드 플러그인을 사용합니다.

언급URL : https://stackoverflow.com/questions/9907858/how-to-add-a-field-in-edit-post-page-inside-publish-box-in-wordpress

반응형