将pdf文件附加到自定义帖子类型

时间:2013-06-21 作者:belacqua16

我正在尝试将pdf文件附加到自定义帖子类型<我在wpsnipp找到了这个片段。com
代码起到了作用,但只适用于帖子
我有一个名为“事件”的自定义帖子类型,我无法使其正常工作。

add_action("admin_init", "pdf_init");
add_action(\'save_post\', \'save_pdf_link\');
function pdf_init(){
    add_meta_box("my-pdf", "PDF Document", "pdf_link", "post", "normal", "low");
    }
function pdf_link(){
    global $post;
    $custom  = get_post_custom($post->ID);
    $link    = $custom["link"][0];
    $count   = 0;
    echo \'<div class="link_header">\';
    $query_pdf_args = array(
            \'post_type\' => \'attachment\',
            \'post_mime_type\' =>\'application/pdf\',
            \'post_status\' => \'inherit\',
            \'posts_per_page\' => -1,
            );
    $query_pdf = new WP_Query( $query_pdf_args );
    $pdf = array();
    echo \'<select name="link">\';
    echo \'<option class="pdf_select">SELECT pdf FILE</option>\';
    foreach ( $query_pdf->posts as $file) {
       if($link == $pdf[]= $file->guid){
          echo \'<option value="\'.$pdf[]= $file->guid.\'" selected="true">\'.$pdf[]= $file->guid.\'</option>\';
             }else{
          echo \'<option value="\'.$pdf[]= $file->guid.\'">\'.$pdf[]= $file->guid.\'</option>\';
             }
            $count++;
    }
    echo \'</select><br /></div>\';
    echo \'<p>Selecting a pdf file from the above list to attach to this post.</p>\';
    echo \'<div class="pdf_count"><span>Files:</span> <b>\'.$count.\'</b></div>\';
}
function save_pdf_link(){
    global $post;
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE){ return $post->ID; }
    update_post_meta($post->ID, "link", $_POST["link"]);
}
add_action( \'admin_head\', \'pdf_css\' );
function pdf_css() {
    echo \'<style type="text/css">
    .pdf_select{
            font-weight:bold;
            background:#e5e5e5;
            }
    .pdf_count{
            font-size:9px;
            color:#0066ff;
            text-transform:uppercase;
            background:#f3f3f3;
            border-top:solid 1px #e5e5e5;
            padding:6px 6px 6px 12px;
            margin:0px -6px -8px -6px;
            -moz-border-radius:0px 0px 6px 6px;
            -webkit-border-radius:0px 0px 6px 6px;
            border-radius:0px 0px 6px 6px;
            }
    .pdf_count span{color:#666;}
            </style>\';
}
function pdf_file_url(){
    global $wp_query;
    $custom = get_post_custom($wp_query->post->ID);
    echo $custom[\'link\'][0];
}
并在主题中显示链接:

<? pdf_file_url(); ?>
我将帖子类型从“post”更改为“events”,元框出现在“events”页面中<到目前为止还不错
它确实在第一次保存pdf。它还显示正确的链接<但是如果我尝试更改pdf,它不会保存它。它再次显示第一个

3 个回复
SO网友:ksr89

这是我在wordpress网站自定义帖子类型中使用的代码,用于将pdf添加到帖子中,我尝试更改并使用它。希望能有所帮助

function add_custom_meta_boxes() {  
    add_meta_box(\'wp_custom_attachment\', \'Hotel Brochure\', \'wp_custom_attachment\', \'hotel_post\', \'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">\';
    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 {
                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\');
获取PDF

$hotel_brochure = get_post_meta( $post_id, \'wp_custom_attachment\', true );
$hotel_brochure[\'url\']

SO网友:Roger Waterstone

昨天晚上,我使用上面的代码(和额外的注释)完成了这项工作,我想在这里添加一些东西。首先,在“获取PDF”部分,“wpcf手册”应更改为“wp\\U custom\\U attachment”。接下来,回答OP的最后一个问题,让它出现在后端,下面是我使用的技巧。在“echo$html在函数中。php,添加:

$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>\';
}

SO网友:leaf

对于那些想fixfix 公认答案:

define(\'PREFIX\', \'a-good-prefix-\');
echo \'<input type="file" name="\' . PREFIX . \'attachment">\';
add_action(\'save_post\', function ($post_id) {
  $key = PREFIX . \'attachment\';
  if (empty($_FILES[$key][\'name\'])) return;
  if (metadata_exists(\'post\', $post_id, $key)) {
    unlink(get_post_meta($post_id, $key, true)[\'file\']);
  }
  $name = $_FILES[$key][\'name\'];
  $tmp = $_FILES[$key][\'tmp_name\'];
  $bits = file_get_contents($tmp);
  $upload = wp_upload_bits($name, null, $bits);
  if (isset($upload[\'error\']) && $upload[\'error\'] !== false) {
    wp_die(\'Upload error: \' . $upload[\'error\']);
  } else {
    update_post_meta($post_id, $key, $upload);
  }
});
实际来源:https://code.tutsplus.com/articles/attaching-files-to-your-posts-using-wordpress-custom-meta-boxes-part-1--wp-22291.

结束

相关推荐

对外部PHP脚本中GET_POSTS返回的帖子进行计数

我使用外部PHP脚本中的WP,包括wp-load.php 文件到目前为止,所有功能和一切都按预期工作,除了一件事:我无法获得$wp_query->found_posts 在我用get_posts() 作用有什么提示我应该用什么来代替吗?谢谢