2012-03-28 7 views
18

मैं पोस्ट पेज को जोड़ने/संपादित करने में प्रकाशित ब्लॉक के अंदर एक नया चेकबॉक्स फ़ील्ड जोड़ना चाहता हूं। क्या किसी को पता है कि यह कैसे करना है?वर्डप्रेस में प्रकाशित बॉक्स के अंदर संपादन पोस्ट पेज में कोई फ़ील्ड कैसे जोड़ें?

+0

चेक इस -> http://wordpress.org/extend/plugins/more-fields/ – Rikesh

+0

अधिक मैं जाँच कर ली है खेत। क्या हम add_meta_box() जैसे किसी अन्य तरीके से एक नया क्षेत्र नहीं जोड़ सकते हैं? – rbncha

उत्तर

20

मैं अंत में समाधान मिल गया है। मुझे आशा है कि यह किसी के लिए अच्छा इस्तेमाल होगा।

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']); 
} 
+0

में ** प्रकाशित ** ब्लॉक के अंदर एक चेकबॉक्स जोड़ने का कोई तरीका नहीं मिला है। मैंने कभी उस बॉक्स में कुछ डालने का सोचा नहीं था, लेकिन ऐसा करने की (कुछ हद तक अनियंत्रित) विधि अच्छी है। बाद में उपयोग के लिए इसे तारांकित किया! –

+2

बस एक छोटा अपडेट: 'publ_in_frontpage() 'को कोई पैरामीटर नहीं मिल रहा है, इसलिए मैंने' वैश्विक $ पोस्ट' का उपयोग किया, अंदर, ठीक काम करता है। – frnhr

+0

यह कोड पूर्ण/सही से बहुत दूर है और यह काम नहीं कर रहा है, मुझे संदेह है कि यह कभी भी काम करता है। मैंने नीचे अपना निश्चित/टिप्पणी कोड जोड़ा। – user2019515

2

अच्छा !, मुझे में फ़ील्ड जोड़ने का कोई समाधान नहीं मिला, प्रकाशित करें। अस्थायी समाधान के लिए, मैंने नीचे दिए गए सरल कोड जोड़कर नया ब्लॉक जोड़ा है।

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']); 
} 

12

rbncha के कोड बॉक्स से बाहर काम नहीं किया और फेरबदल का एक बहुत जरूरत है, नीचे दिए गए कोड क्या मैं के साथ आया है। मैंने कुछ टिप्पणियां जोड़ दी हैं जो सबकुछ अच्छी तरह से बताती हैं।

निम्नलिखित कोड पोस्ट के प्रकाशित ब्लॉक में चेकबॉक्स जोड़ता है (आप आसानी से पोस्ट प्रकार बदल सकते हैं), और डेटाबेस में/से मूल्य को स्टोर/पुनर्प्राप्त करता है। कुछ मामूली tweaking के साथ आप आसानी से एक टेक्स्ट फ़ील्ड या अपनी पसंद के कुछ भी जोड़ सकते हैं।

यह ध्यान दिया जाना चाहिए कि आपको 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

+0

'जांचें कि कस्टम फ़ील्ड सबमिट किया गया है' आप उसी नाम और मूल्य = 0 – gaRex

+0

के साथ चेकबॉक्स से पहले छिपे हुए फ़ील्ड को जोड़ सकते हैं जो खराब अभ्यास की तरह लगता है? हैकी समाधान? – user2019515

+0

नहीं - कई सारे ढांचे ऐसा करते हैं। तो इसे मौजूदा अभ्यास के रूप में नामित किया जा सकता है। – gaRex

संबंधित मुद्दे