2016-08-13 8 views
2

投稿内容からおすすめ画像を追加しようとしていました。私の考えは、コンテンツの最初のイメージを投稿の特集画像として設定することです。おすすめコンテンツをプログラムで投稿コンテンツを設定する

私はこれは私がこれをしたいかだけの考えである

add_action('publish_post', 'auto_featured_image_publish_post'); 
     function auto_featured_image_publish_post($post, $post_id) { 
      $thumnail_id = //something I need help with 
      set_post_thumbnail($post, $thumbnail_id); 
     } 

funtions.phpでこれを書いています。

助けてください。おかげ

答えて

1

チェックこのURL

https://www.gavick.com/blog/wordpress-quick-tip-4-automatically-set-the-first-post-image-as-a-featured-image

たり、任意のHTMLコードから、あなたのクイックヘルプのための

<?php 
$postcontent = ""; 
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $postcontent, $image); 
// you can use the exist pattern or use this 
// '/< *img[^>]*src *= *["\']?([^"\']*)/i' 
echo $image['src']; 
?> 
+1

感謝を第1の画像のURLを取得するには、このコードを使用することができます。 正常に動作するためには、画像添付IDを入力する必要があると思います。あなたはそれについて何か考えていますか? thumbnail_idは整数でなければなりません。 https://codex.wordpress.org/Function_Reference/set_post_thumbnail –

+1

このURLをチェックするhttps://www.gavick.com/blog/wordpress-quick-tip-4-automatically-set-the-first-post-image-as -a-featured-image – Mostafa

+1

Mostafa Bhaiありがとうございました。あなたは私の日を救った:) –

関連する問題