2016-02-02 19 views
1

我正在尋找一種方法來顯示自定義字段/元框的修改日期。直到現在,我只知道如何回聲獲取後期元框的修改日期

the_modified_date('l, j.n.Y'); 

但這隻適用於整個職位。使用附加的PDF文件上傳一個meta_box

我',代碼:

<?php class PDF_Metabox_id1 { 

function __construct() { 
add_action('post_mime_types',   array($this, 'pdf_mime_type_id1' ) ); 
add_action('add_meta_boxes',   array($this, 'admin_scripts_id1' ), 5); 
add_action('add_meta_boxes',   array($this, 'metabox_add_id1' ) ); 
add_action('save_post',    array($this, 'pdf_save_postdata_id1')); 
add_action('wp_ajax_refresh_pdf',  array($this, 'refresh_pdf_id1') ); } 

function pdf_mime_type_id1() { 
    $post_mime_types['application/pdf'] = array(__('PDFs'), __('Manage PDFs'), _n_noop('PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>')); 
    return $post_mime_types; 
} 

function admin_scripts_id1() { 
    wp_register_script('pdf_metabox_js', get_stylesheet_directory_uri() . '/js/pdf_metabox.js'); 
} 

function metabox_add_id1() { 
    $post_types  = array('myposttype','anotherposttype'); 
    $context = 'normal'; $priority  = 'low'; 
    foreach($post_types as $post_type) { 
     add_meta_box('pdf_metabox_id1', __('PDF Box 1', 'pdf_metabox_id1'), array($this, 'pdf_metabox_id1'), $post_type, $context, $priority); 
     wp_enqueue_media(); 
     wp_enqueue_script('pdf_metabox_js'); 
    } 
} 

function pdf_metabox_id1($post) { 
    $original_post = $post; echo $this->pdf_metabox_html_id1($post->ID); $post = $original_post; 
} 

function pdf_item_id1($id) { 
    if(!$id) return; $pdf_url = esc_url_raw(wp_get_attachment_url($id)); $pdf = $pdf_url; return $pdf; 
} 

function pdf_metabox_html_id1($post_id) { 
    $current_value = ''; $post_meta = get_post_custom($post_id); 
    if(isset($post_meta['pdf_id_id1'][0])) $current_value = $post_meta['pdf_id_id1'][0]; 
    $return = ''; wp_nonce_field(plugin_basename(__FILE__), 'pdf_noncename'); 
    $return .= '<p>'; 
    $return .= '<a title="'.__('PDF', 'pdf_metabox_id1').'" class="button button-primary insert-pdf-button" id="insert_pdf_button_id1" href="#" style="float:left">'.__('Upload/editiere PDF', 'pdf_metabox_id1').'</a><span id="pdf_spinner_id1" class="spinner" style="float:left"></span></p>'; 
    $return .= '<div style="clear:both"></div>'; 
    $return .= '<input type="hidden" name="pdf_id_id1" id="pdf_id_id1" value="'.$current_value.'">'; 
    $return .= '<div style="clear:both"></div>'; 
    $return .= '<div id="pdf_wrapper_id1">'; 
    $pdf = $this->pdf_item_id1($current_value); if(empty($pdf)) { 
     $return .= '<p>Diesem Feld ist kein PDF hinterlegt.</p>'; } else { 
      $return .= '<br />URL des PDF:<br /> '; 
      $return .= $pdf; } 
    $return .= '</div>'; 
    return $return; 
} 

function pdf_save_postdata_id1($post_id){ 
    if (isset($_POST['post_type']) && 'post' == $_POST['post_type']) { 
     if (!current_user_can('edit_post', $post_id)) return; } if (!isset($_POST['pdf_noncename']) || ! wp_verify_nonce($_POST['pdf_noncename'], plugin_basename(__FILE__))) return; 
     if(isset($_POST['pdf_id_id1'])): update_post_meta($post_id, 'pdf_id_id1', sanitize_text_field($_POST['pdf_id_id1'])); 
     else: if (isset($post_id)) { 
      delete_post_meta($post_id, 'pdf_id_id1'); } 
     endif; 
} 

function refresh_pdf_id1() { 
    if(isset($_POST['id'])){ 
     $item = $_POST['id']; 
     if($item != '' && $item !=0){ 
      $pdf = $this->pdf_item_id1($item); 
      $ret = array(); 
      if(!empty($pdf)) {$ret['success'] = true; 
      $ret['pdf'] = $pdf; } else { 
       $ret['success'] = false; } } else { 
        $ret['success'] = true; $ret['pdf'] = ''; } } else { 
         $ret['success'] = false; } echo json_encode($ret); die(); 
         } 
} 

$PDF_Metabox_id1 = new PDF_Metabox_id1(); 

在我的主題我使用:

$post_meta_id1 = get_post_custom(get_the_ID()); 
$pdf_id_id1 = $post_meta_id1['pdf_id_id1'][0]; 
$pdf_url_id1 = wp_get_attachment_url($pdf_id_id1); 

...現在我要顯示的此字段的修改日期。有任何想法嗎?

+0

你是什麼意思的'修改日期後元框'你怎麼能得到修改日期/更新日期,而你沒有任何日期選擇器或日期輸入在metabox。可能你想獲得發佈修改日期? – Noman

+0

我爲自定義帖子類型使用了15個自定義字段/元框。有時只是其中一個字段會被更新,但不會發布內容。我想顯示更新的自定義字段的日期。 高優先級有這個pdf功能以上.. – Cycle99

+0

然後什麼是修改日期字段的名稱?以及當自定義字段得到更新時,如何保存修改日期。上面的代碼使用較少的問題。你需要添加與你的問題相關的代碼。 – Noman

回答

0

按照註釋

how do i save the modified date of this custom field and how do i get it? 

您可能需要通過一個檢查循環中的每一個自定義字段,如果任何自定義字段的不列入當前職位數據進行匹配,然後一個新的自定義字段保存到post_meta。然後檢索您的template/page

只是一個想法:

<?php 
function attributes_save_postdata($post_id) { 

    $postcustom = get_post_custom($post_id); 
    $is_modified = false; 
    foreach ($postcustom as $key => $val) { 
     var_dump($val); 
     // check your post custom values and 

     $getpostmeta = get_post_meta($post_id, 'your_meta_key', true); 

     // if database value ($getpostmeta) not equal to current post value($_POST['your_meta_key']) 
     if ($getpostmeta != $_POST['your_meta_key']) { 
      $is_modified = true; 
      // if found any custom field updated set $modified = true 
     } 
    } 

    // if found any custom field updated add or update new date and time 
    if ($is_modified) { 
     $date = date('Y-m-d h:i:s'); // example : 2016-02-02 12:00:00 current date and time 
     update_post_meta($post_id, '_modifieddate', $date); 
    } 
} 

add_action('save_post', 'attributes_save_postdata'); 

然後在你的主題。獲取修改日期。

$post_id = get_the_ID(); 
$getpostmeta = get_post_meta($post_id, 'your_meta_key', true); 
0

我認爲,即使是自定義字段,關於此的wordpress內置函數也會幫助您。

<p>Last modified: <?php the_modified_time(); ?></p> 
相關問題