Feb 012009
 

久しぶりにWordPressを触ってる。
で、画像ファイルのアップロード先を記事別に分類しようと思ったのだが、うまい方法が思いつかない。

Custom Upload Dirプラグインを使えば、
wp-content/uploads/%year%/%month%/%date%/%post_title%/
の形式にはできるのだが、日本語タイトルだとデフォルトのポストスラグがUTF-8にエンコードされた形式になってしまい、アップロードはうまくいくものの画像表示ができなくなってしまう。

wp-content/uploads/%year%/%month%/%date%/%post_id%/
の形式に設定できればいいのにと、以下のパッチをあてて、post_title部分がpost_idになるように強制してしまった。

--- custom_upload_dir.php.orig  2009-01-31 21:19:00.000000000 +0900
+++ custom_upload_dir.php       2009-01-31 22:23:02.437500000 +0900
@@ -161,13 +161,7 @@
        $customdir = '';
        $cur_post = get_post($post_id);
        $postGotDate = ($cur_post->post_date != "0000-00-00 00:00:00" && $cur_post->post_date);
-       $name = $cur_post->post_name;//post_name == slug
-       if(empty($name)){
-               $name = sanitize_title($cur_post->post_title);
-       }
-       if(empty($name)){
-               $name = $cur_post->ID;
-       }
+       $name = $cur_post->ID;
        if($postGotDate) { //only use the post_date if it has been defined (ie. published or manually altered)
                $time = strtotime($cur_post->post_date);
                if($use_yearmonth){ //custom upload dir put stuff according to the post's timestamp. If yearmonth is on, we need to override it.

これでプラグインの設定でpost_titleに設定したフィールドにはpost_idが代入されるようになる。
本当はプラグイン設定でpost_idを選択できるようにするのがよいとは思いつつ、当面これでごまかすつもり。

Sorry, the comment form is closed at this time.