DL版と物販版の商品画像の出力の違いについて

フォーラム その他 DL版と物販版の商品画像の出力の違いについて

  • このトピックには1件の返信、1人の参加者があり、最後にmiyaにより6年、 3ヶ月前に更新されました。
2件の投稿を表示中 - 1 - 2件目 (全2件中)
  • 投稿者
    投稿
  • #84848
    miya
    参加者

    物販版では、imgを出力する時に「srcset=」というのが追加されてオリジナルよりも小さいサイズが表示されますが、DL版では、「srcset=」が表示されずに、オリジナルの画像が表示されてしまいます。
    DL版では、自動で画像を登録しているためそちらが原因かもしれませんので一応コードを貼ります。わかる方居ましたら教えてほしいです。

    物販の商品を登録した時に自動で、ダウンロード版の商品を作るコードです。

    
    // Welcart物販の記事を投稿した時に、ダウンロード版の記事を自動で投稿する。
    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; //記事本文取得
                    global $usces;
                    $get_sku_code = $usces->get_skus($post->ID, 'code'); //skuコードからsku情報取得
                    $skus = $usces->get_skus($post->ID, 'code');
    
                    foreach ((array) $skus as $sku) {
                        $array = $sku['code'];
                    }
                    $my_post = array(
                        'post_type' => 'post', //投稿タイプ
                        'post_title' => $get_name, //記事タイトル
                        'post_status' => 'publish', //投稿ステータス
                        'post_content' => $get_content, //記事本文
                        'post_category' => array($category_slug), //記事カテゴリー
                        'tags_input' => $get_tag_ids, //タグ
                        'post_name' => 'd-' . $get_code, //スラッグ
                    );
                    $post_id = wp_insert_post($my_post);
    
                    update_post_meta($post_id, '_itemCode', $get_codes); // 商品コード
                    update_post_meta($post_id, '_itemName', $get_name); // 商品名
                    update_post_meta($post_id, '_item_division', 'data'); //区分コンテンツファイル
                    $replace_codes = str_replace('d-', '', $get_codes);
                    update_post_meta($post_id, '_dlseller_file', $replace_codes . '.jpg'); //ファイル名
    //    usces_update_sku($post_id, $sku_code, 'stocknum', ''); //SKU在庫数
                    usces_update_sku($post_id, $sku_code, 'name', $skus[$array]['name']); //SKU表示名
                    usces_update_sku($post_id, $sku_code, 'code', $array); //SKUコード
    
                    //メディアライブラリにタイトルを変更した画像を追加
                    $wp_upload_dir = wp_upload_dir(); //ファイルのアップロード先ディレクトリを取得
                    $filename = $wp_upload_dir['url'] . '/' . $get_code . '.jpg'; //商品コードから読み込むファイルの指定
    
                    $wp_filetype = wp_check_filetype(basename($filename), null); //ファイル名の拡張子、mime形式を配列で取得
                    $attachment = array(
                    'guid' => $wp_upload_dir['url'] . '/d-' . basename($filename),
                    'post_mime_type' => $wp_filetype['type'], //mime形式を指定「image/png」「image/jpeg」など
                    'post_title' => 'd-' . $get_code, //ファイル名を指定
                    'post_content' => '', //空の文字列を指定
                    'post_status' => 'inherit', //投稿ステータスを指定
                    );
                    vd($attachment);
                    $attach_id = wp_insert_attachment($attachment, $filename, $post->ID); //添付ファイルをメディアライブラリにアップロード。変数「attach_id」に返ってきたIDを格納
    
                }
            }
        }
    }
    
    add_action('save_post', 'get_article_created_time');
    
    #84856
    miya
    参加者

    自己解決しました。

2件の投稿を表示中 - 1 - 2件目 (全2件中)
  • このトピックに返信するにはログインが必要です。