【问题标题】:get the modified date of post meta box获取发布元框的修改日期
【发布时间】:2016-02-02 06:07:32
【问题描述】:

我正在寻找一种方法来显示自定义字段/元框的修改日期。到现在,我只知道怎么回声

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

...现在我想显示该字段的修改日期。有什么想法吗?

【问题讨论】:

  • modified date of post meta box 是什么意思,当元框中没有任何日期选择器或日期输入时,如何获得修改日期/更新日期。可能你想得到修改后的日期?
  • 我正在为自定义帖子类型使用 15 个自定义字段/元框。有时只会更新其中一个字段,但不会更新帖子内容。我想显示更新自定义字段的日期。高优先级有上面这个pdf-function..
  • 那么修改日期字段的名称是什么?以及当自定义字段更新时如何保存修改日期。根据问题,上面的代码使用较少。您需要添加与您的问题相关的代码。
  • 这就是问题,对不起,我的错误 :-) 我如何保存此自定义字段的修改日期以及如何获取它?

标签: php wordpress date custom-fields


【解决方案1】:

根据评论

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

【讨论】:

    【解决方案2】:

    我认为即使是自定义字段,wordpress 的内置功能也会为您提供帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      相关资源
      最近更新 更多