0
我有一個函數在這裏將抓住兩個最近的帖子,並抓住張貼在這兩個職位內的圖像。問題是,雖然,它不斷抓住了第一個帖子的第一個圖像,而不是也是第二:抓取張貼圖像 - WordPress的PHP
代碼:
function getImageFeatured($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
$more = 0;
}
$args = array('numberposts' => '2');
$recent_posts = wp_get_recent_posts($args);
foreach($recent_posts as $recent){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . getImageFeatured('1') .'</a></li> ';
}
不要知道爲什麼它只是發出了第一篇文章的第一張圖像。
建議和想法讚賞。
編輯:
隨着修訂:
.....above function I posted....
global $post;
$args = array('numberposts' => '2');
$recent_posts = wp_get_recent_posts($args);
foreach($recent_posts as $post){
setup_postdata($post);
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();
這不工作對我來說無論是。看來如果我使用setup_postdat...
它不會處理,但如果我刪除它,它會通過但不會做我所問,但是我所說的問題是。
你能給我爲我的功能有一定的技巧有待改進? –
此外,這對我來說根本不起作用 –
您可以發佈包含我的修訂版的完整代碼嗎? –