4 人参与回答
戒赌吧
戒赌吧 · 寻渠道推广 戒赌吧 网站

在中,通过get_queried_object_id()获取当前附件的ID

再用wp_get_post_parent_id( $ID )来获取当前附件所属的日志ID。

最后用get_children来获取日志下的所有附件即可。

中的大致代码如下:(不要放在循环内)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$current_image_id = get_queried_object_id();
$parent_id = wp_get_post_parent_id( $current_image_id );
        /* 获取指定post下的所有图片附件. */
        $attachments = get_children(
            array(
                'post_parent'      => (array)$parent_id,
                'post_status'      => 'inherit',
                'post_type'        => 'attachment',
                'post_mime_type'   => 'image',
                'order'            => 'ASC',
                'orderby'          => 'menu_order ID',
                'suppress_filters' => true
            )
        );
    if count$attachments ) ) {
        foreach( .... ){}
    }

淘宝找技术

风逝
风逝 · seo优化,linux运维,前端页面修改。

找个技术搞下,自己研究半天

崔家大院
崔家大院 · 网站诊断、建站、快速排名

<ol><li><p>功能代码:</p><pre>/*
WordPress 首页输出文章所有图片缩略图
*/
function all_img($soContent){
$soImages ='~]
\ />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics);
if( $allPics > 0 ){
foreach($thePics[0] as $v){
echo $v;
}
}
else {
echo "";
}
}</pre><p>上面的代码会导致最后输出的图片全部是原大小尺寸,所以我们需要在输出图片时为其加上大小 style 的限制,所以需要改动一些代码;将上面第 16 行的代码改为:</p><pre><ol><li><echo “/images/thumb.gif’ style=”width:278px;height:122px;”>”;</li></ol></pre><p>但当给予了 style 限制之后,图片的大小虽然都固定了下来,但却出现了图片的失真,因此我们还是最好使用 css 的方法去限制输出图片的大小,css 具体的代码就先不贴出来了,大家也可以自己去捯饬一下。</p><p>代码调用:</p><pre><ol><li><</li><li>PHP all_img($post->post_content);</li><li>></li></ol></pre><p>在模板文件中需要调用的地方使用上面的代码即可。</p><p>xiu 主题是在该基础上添加了判断语句,图片大于等于 1 张小于 4 张就只输出 1 张图片、大于等于 4 张小于 8 张就输出 4 张图片,大于 8 张就输出 8 张,一共用到 2 个函数:</p><p>post excerpt</p><p>post thumbnail</p><p>有兴趣的朋友可以进一步折腾。</p><p>2016 年 4 月 12 日更新</p><p>用了一段 xiu 主题后,发现很多代码改改还是蛮好用的。现在讲 xiu 主题实现方法分享出来,已经针对性的修改了,适用于其他主题</p><p>将下面代码加入到主题 文件中</p><pre><ol><li> function hui_get_thumbnail( $single=true, $must=true ) {</li><li> global $post;</li><li> $html = '';</li><li> if ( has_post_thumbnail() ) {</li><li> $domsxe = simplexml_load_string(get_the_post_thumbnail());</li><li> $src = $domsxe->attributes()->src;</li><li> $src_array = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');</li><li> $html = sprintf('<li></li>', $src_array[0]);</li><li> } else {</li><li> $content = $post->post_content;</li><li> preg_match_all('//sim', $content, $strResult, PREG_PATTERN_ORDER);</li><li> $images = $strResult[1];</li><li> $counter = count($strResult[1]);</li><li> $i = 0;</li><li> foreach($images as $src){</li><li> $i++;</li><li> $src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');</li><li> $src2 = $src2[0];</li><li> if( !$src2 && true ){</li><li> $src = $src;</li><li> }else{</li><li> $src = $src2;</li><li> }</li><li> $item = sprintf('<li></li>', $src);</li><li> if( $single){</li><li> return $item;</li><li> break;</li><li> }</li><li> $html .= $item;</li><li> if(</li><li> ($counter >= 4 && $counter < 8 && $i >= 4) ||</li><li> ($counter >= 8 && $i >= 8) ||</li><li> ($counter > 0 && $counter < 4 && $i >= $counter)</li><li> ){</li><li> break;</li><li> }</li><li> }</li><li> }</li><li> return $html;</li><li> }</li><li> function hui_get_attachment_id_from_src ($link) {</li><li> global $wpdb;</li><li> $link = preg_replace('/-\d+x\d+(=.(jpg|jpeg|png|gif)$)/i', '', $link);</li><li> return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");</li><li> }</li></ol></pre><p>在需要调用的地方:</p><pre><ol><li><?php echo hui_get_thumbnail(false,true);></li></ol></pre></li></ol>

SEO培训招生中
189