$attach_id = wp_insert_attachment($attachment, $uploaded['file'], $post_id);

この写真の情報を入れるときに3番目の引数の値にしたいpostのidを書けば良い。これにより、添付ファイルがそのポストに接続されている。

$uploaded['file']wp_handle_upload()の結果である。

管理者ページメディア項目のファイルに行くと確認することができる。

また、このように接続した奴をクライアント側でインポートするには、次のコードを参考にする。

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
);
$attachments = get_posts($args);

if($attachments){?>
    <p>
        添付ファイル :
        <a href="<?=wp_get_attachment_url($attachments[0]->ID)?>">
            <?=apply_filters('the_title', $attachments[0]->post_title);?>
        </a>
    </p>
<?}?>

- コメント機能はありません。コメントの代わりに[email protected]にメールを送ってください。