简体   繁体   English

在WordPress循环中使用自定义元框日期

[英]Using Custom Meta Box Date in WordPress Loop

I'm using the below script in the functions.php file to upload a PDF to a custom post type, however I am unsure of how to then retrieve the PDF URL within the WordPress Loop: 我正在使用functions.php文件中的以下脚本将PDF上传到自定义帖子类型,但是我不确定如何在WordPress循环中检索PDF URL:

function add_custom_meta_boxes() {  
    add_meta_box('wp_custom_attachment', 'PDF Newsletter', 'wp_custom_attachment', 'newsletter', 'normal', 'high');  
}
add_action('add_meta_boxes', 'add_custom_meta_boxes');  

function wp_custom_attachment() {  
    wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');
    $html = '<p class="description">';
    $html .= 'Upload your PDF here.';
    $html .= '</p>';
    $html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25">';
    $filearray = get_post_meta( get_the_ID(), 'wp_custom_attachment', true );
    $this_file = $filearray['url'];
    if($this_file != ""){
    $html .= '<div>Current file:<br>"' . $this_file . '"</div>';
    }
    echo $html;
}

add_action('save_post', 'save_custom_meta_data');
function save_custom_meta_data($id) {
    if(!empty($_FILES['wp_custom_attachment']['name'])) {
        $supported_types = array('application/pdf');
        $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
        $uploaded_type = $arr_file_type['type'];

        if(in_array($uploaded_type, $supported_types)) {
            $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));
            if(isset($upload['error']) && $upload['error'] != 0) {
                wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
            } else {
                add_post_meta($id, 'wp_custom_attachment', $upload);
                update_post_meta($id, 'wp_custom_attachment', $upload);
            }
        }
        else {
            wp_die("The file type that you've uploaded is not a PDF.");
        }
    }
}

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
}
add_action('post_edit_form_tag', 'update_edit_form');

Here is the loop I am using, including my failed attempt of adding the PDF url: 这是我正在使用的循环,包括添加PDF URL的失败尝试:

                <?php
                $args = array( 'post_type' => 'newsletter', 'posts_per_page' => 10 );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post();
                  the_title();
                  echo '<div class="entry-content">';
                  the_content(); ?>
                   <?php 
                    $news_pdf = get_post_meta( $post_id, 'wp_custom_attachment', true );
                    $news_pdf['url']
                  ?>
                 <?php echo '</div>';
                endwhile;
                ?>

Please first change $post_id to get_the_ID() within your loop. 请先在循环中将 $ post_id更改 get_the_ID()

Change $news_pdf['url'] to: $ news_pdf ['url']更改为:

 echo $news_pdf['url'];

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM