下記の記述は、記事を投稿した時に、タイトルと本文と画像をアップロードするプログラムです。
出来ないこと
下記の画像をリネームしたものを記事更新時に追加したいです。今の記述では、新しく同じ画像が作られますが、ファイル名が同じ状態です。わかる方居ましたら教えてほしいです。
$wp_upload_dir = wp_upload_dir(); //ファイルのアップロード先ディレクトリを取得
$filename = $wp_upload_dir['url'] . '/' . 'テスト.png' ;
function get_article_created_time() { $post_divide = get_post_type(get_the_ID()); if ($post_divide == 'post') { remove_action('save_post', 'get_article_created_time'); //無限loop解除用 if (empty($_POST['publish'])) { } else { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { // 自動保存時 return $post_id; // 何もしない } elseif (!empty($_POST)) { global $post; $get_title = $post->post_title; //記事タイトル取得 $get_content = $post->post_content; //記事本文取得 $my_post = array( 'post_type' => 'post', //投稿タイプ 'post_title' => $get_name, //記事タイトル 'post_status' => 'publish', //投稿ステータス 'post_content' => $get_content, //記事本文 ); $post_id = wp_insert_post($my_post); //メディアライブラリにタイトルを変更した画像を追加 $wp_upload_dir = wp_upload_dir(); //ファイルのアップロード先ディレクトリを取得 $filename = $wp_upload_dir['url'] . '/' . 'テスト.png' ; $wp_filetype = wp_check_filetype(basename($filename), null); //ファイル名の拡張子、mime形式を配列で取得 $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $wp_filetype['type'], //mime形式を指定「image/png」「image/jpeg」など 'post_title' => 'テスト', //ファイル名を指定 'post_content' => '', //空の文字列を指定 'post_status' => 'inherit' //投稿ステータスを指定 ); $attach_id = wp_insert_attachment($attachment, $filename, $post->ID); //添付ファイルをメディアライブラリにアップロード。変数「attach_id」に返ってきたIDを格納 require_once( ABSPATH . '/wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata($attach_id, $filename); //データベース書き込み wp_update_attachment_metadata($attach_id, $attach_data); //データベース書き込み } } } } add_action('save_post', 'get_article_created_time');

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/12/20 05:43