如果想用wordpress做成图片站或者cms,云顶国际主页可能需要获取文章所有图片,那么如何实现呢?下面教大家wordpress获取文章所有图片和数量方法:
wordpress主题获取文章中的所有图片
function hui_get_thumbnail( $single=true, $must=true ) {
global $post;
$html = '';
if ( has_post_thumbnail() ) {
$domsxe = simplexml_load_string(get_the_post_thumbnail());
$src = $domsxe->attributes()->src;
$src_array = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
$html = sprintf('', $src_array[0]);
} else {
$content = $post->post_content;
preg_match_all('//sim', $content, $strresult, preg_pattern_order);
$images = $strresult[1];
$counter = count($strresult[1]);
$i = 0;
foreach($images as $src){
$i ;
$src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
$src2 = $src2[0];
if( !$src2 && true ){
$src = $src;
}else{
$src = $src2;
}
$item = sprintf('', $src);
if( $single){
return $item;
break;
}
$html .= $item;
if(
($counter >= 4 && $counter < 8 && $i >= 4) ||
($counter >= 8 && $i >= 8) ||
($counter > 0 && $counter < 4 && $i >= $counter)
){
break;
}
}
}
return $html;
}
function hui_get_attachment_id_from_src ($link) {
global $wpdb;
$link = preg_replace('/-\d x\d (?=\.(jpg|jpeg|png|gif)$)/i', '', $link);
return $wpdb->get_var("select id from {$wpdb->posts} where guid='$link'");
}
wordpress主题前台调用代码放在需要显示的位置上
获取wordpress文章内容中的图片数量值
if( !function_exists('get_post_images_number') ){
function get_post_images_number(){
global $post;
$content = $post->post_content;
preg_match_all('//sim', $content, $result, preg_pattern_order);
return count($result[1]);
}
}
wordpress前台显示代码
# 更多,请关注「wordpress专题」
方法来源:wazhuti