特定のカテゴリの特定の投稿の最初の画像を表示する必要があるサイトがあります。私はそれを動作させており、以下のコードを追加しました。私の質問は、もっと良い方法はありますか?これはひどく厄介なようです。
src="
との間に最初に表示されるものについて、投稿データを解析しています"
。私が行方不明になっている落とし穴はありますか?Wordpressにはこれを組み込むためのより良い方法が組み込まれていますか?
function extractStringFromString($string, $start, $end) {
//Finds the first string between $start and $end.
$startPos = strpos($string,$start);
$stringEndTagPos = strpos($string,$end,$startPos);
$stringBetween = substr($string,$startPos+strlen($start),$stringEndTagPos-$startPos-strlen($start));
if (strlen($stringBetween) != 0 && $startPos!= '') {
return $stringBetween;
}
else {
return false;
}
}
function getfirstimage($post){
//Returns url of first image located in post data
global $wpdb;
$sqlquery = "SELECT post_content FROM `wp_posts` WHERE `ID` = $post LIMIT 0 , 1";
$result = $wpdb->get_results( $sqlquery );
$result = $result[0];
$postcontent = $result->post_content;
if ($result){
return extractStringFromString($postcontent, 'src="', '" ');
}
else return 0;
}