获取发布元框的修改日期

时间:2016-02-02 06:07:32

标签: php wordpress date custom-fields

我正在搜索显示自定义字段/元框的修改日期的方法。直到现在,我只知道如何回应

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

但这只适用于整个帖子。

我正在使用meta_box上传其他pdf文件,代码:

<?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);

...现在我想显示此字段的修改日期。任何想法?

2 个答案:

答案 0 :(得分: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 );

答案 1 :(得分:0)

我认为关于这个的wordpress内置函数可以帮助你甚至自定义字段。

<p>Last modified: <?php the_modified_time(); ?></p>