WordPress无插件实现带缩略图相关内容调用
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

蜗牛在\\”WordPress无插件实现相关内容调用功能\\”文章中分享到相关文章调用,但是如果有需要调用图片的得需要找到其他方法。这里在WP大学看到有调用相关文章的时候可以带有图片,这个就比较适合有需要图片相关文章的调用功能。如果我们有需要的可以参考,同样是无插件实现的。
第一、脚本部分
A – 定义内容
//添加特色缩略图支持
if ( function_exists(\\’add_theme_support\\’) )add_theme_support(\\’post-thumbnails\\’);
//输出缩略图地址
function post_thumbnail_src(){
global $post;
if( $values = get_post_custom_values(\\”thumb\\”) ) { //输出自定义域图片地址
$values = get_post_custom_values(\\”thumb\\”);
$post_thumbnail_src = $values [0];
} elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),\\’full\\’);
$post_thumbnail_src = $thumbnail_src [0];
} else {
$post_thumbnail_src = \\’\\’;
ob_start();
ob_end_clean();
$output = preg_match_all(\\’/<img.+src=[\\\\\\’\\”]([^\\\\\\’\\”]+)[\\\\\\’\\”].*>/i\\’, $post->post_content, $matches);
$post_thumbnail_src = $matches [1] [0]; //获取该图片 src
if(empty($post_thumbnail_src)){ //如果日志中没有图片,则显示随机图片
$random = mt_rand(1, 10);
echo get_bloginfo(\\’template_url\\’);
echo \\’/images/pic/\\’.$random.\\’.jpg\\’;
//如果日志中没有图片,则显示默认图片
//echo \\’/images/default_thumb.jpg\\’;
}
};
echo $post_thumbnail_src;
}
将脚本添加到当前主题Functions.php文件中。默认是从缩略图、文章第一张图片调用的图片。如果没有图片我们也可以自定义到/images/default_thumb.jpg设置默认图片。
B – 调出部分
<h3>您可能还喜欢这些内容:</h3>
<ul class=\\”related_img\\”>
<?php
$post_num = 4;
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = \\’\\’; foreach ( $posttags as $tag ) $tags .= $tag->term_id . \\’,\\’;
$args = array(
\\’post_status\\’ => \\’publish\\’,
\\’tag__in\\’ => explode(\\’,\\’, $tags),
\\’post__not_in\\’ => explode(\\’,\\’, $exclude_id),
\\’caller_get_posts\\’ => 1,
\\’orderby\\’ => \\’comment_date\\’,
\\’posts_per_page\\’ => $post_num
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li class=\\”related_box\\” >
<div class=\\”r_pic\\”>
<a h=\\”<?php the_permalink(); ?>\\” title=\\”<?php the_title(); ?>\\” target=\\”_blank\\”>
<img src=\\”<?php echo post_thumbnail_src(); ?>\\” alt=\\”<?php the_title(); ?>\\” class=\\”thumbnail\\” />
</a>
</div>
<div class=\\”r_title\\”><a h=\\”<?php the_permalink(); ?>\\” title=\\”<?php the_title(); ?>\\” target=\\”_blank\\” rel=\\”bookmark\\”><?php the_title(); ?></a></div>
</li>
<?php
$exclude_id .= \\’,\\’ . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) {
$cats = \\’\\’; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . \\’,\\’;
$args = array(
\\’category__in\\’ => explode(\\’,\\’, $cats),
\\’post__not_in\\’ => explode(\\’,\\’, $exclude_id),
\\’caller_get_posts\\’ => 1,
\\’orderby\\’ => \\’comment_date\\’,
\\’posts_per_page\\’ => $post_num – $i
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li class=\\”related_box\\” >
<div class=\\”r_pic\\”>
<a h=\\”<?php the_permalink(); ?>\\” title=\\”<?php the_title(); ?>\\” target=\\”_blank\\”>
<img src=\\”<?php echo post_thumbnail_src(); ?>\\” alt=\\”<?php the_title(); ?>\\” class=\\”thumbnail\\” />
</a>
</div>
<div class=\\”r_title\\”><a h=\\”<?php the_permalink(); ?>\\” title=\\”<?php the_title(); ?>\\” target=\\”_blank\\” rel=\\”bookmark\\”><?php the_title(); ?></a></div>
</li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo \\\'<div class=\\”r_title\\”>没有相关文章!</div>\\’;
?>
</ul>
将代码添加到模板需要调出的位置,post_num设置调用数量。
第二、设置样式
.related_posts{margin-top:5px;}
.related_img{width:600px;height:210px;}
.related_box{float:left;overflow:hidden;margin-top:5px;width:148px;border-right:1px #eee solid}
.related_box:hover{background:#f9f9f9}
.related_box .r_title{width:auto;height:72px;font-weight:400;font-size:14px;margin:0 10px;overflow:hidden;}
.related_box .r_pic{margin:6px}
.related_box .r_pic img{width:130px;height:100px;border:1px solid #e1e1e1;background:#fff;padding:2px}
根据实际调出情况设置样式。
文章参考自WP大学:https://www.wpdaxue.com/wordpress-related-posts.html
[WordPress相关文章]历史优惠活动内容
猜你可能想看的VPS
- 疯狂猜成语 图猜成语一把尺子上面一个入一个出是什么成语?全球[VPS测评]
- CrownCloud→$7 月 4 核 4GB 内存 60GB SSD 虚拟空间(主机)
- 彩虹数据→350 元 年 1GB 内存 20GB SSD 空间 500G虚拟空间(主机)
- TripodCloud→圣何塞 CN2 GIA 线路 KVM 年付 38全球[VPS测评]
- 一体云→镇江挂机宝 2H1G 3 元 月 高防服务器 16 核 32GB全球[VPS测评]
- STSDUST→$5.33 月 1GB 内存 20GB SSD 空间 1虚拟空间(主机)
- [黑五]GreenCloudVPS→$28.8 年 KVM-1GB 20日本VPS[主机]
- MySQL 修改字段类型或长度全球[VPS测评]
- hostflyte→新上洛杉矶 CN2 GT 线路 免费 WindowsWINDOWS
- ShockHosting→3.74 美元 KVM 1G 150G 1TB全球[VPS测评]
- Navicat 导入 mysql 提示 Err 2006-MySQL s全球[VPS测评]
- 疯狂猜成语 图猜成语屋子里有一只手在勾引狼是什么成语?全球[VPS测评]
- SoftShellWeb→$12.99 年 256MB 内存 10GB 虚拟空间(主机)
- Krypt G2-和 C2-系列的美国云和新加坡云七折 独立服务器八折独立服务器[U]
- CloudIPLC 泉州电信CN2 大带宽出国优质线路 ¥560起/月全球[VPS测评]
- Uluhost → 1.5$ 月 土耳其 1C2G25G硬盘 1Gbps全球[VPS测评]
- 人们为啥不爱吃海底捞了?海底捞上半年亏损达2.97亿 网友神评扎心全球[VPS测评]
- 云计算的大门依旧没向雷军敞开全球[VPS测评]
- 文物、沉船、村落!干旱导致大量遗迹重见天日 2022年干旱还要持续多久?全球[VPS测评]
- 硅云服务器拼团活动:香港云服务器1核1G,拼团价仅98元/年香港VPS[主机]
- 微基主机:1核1G香港云服务器,100Mbps/香港CN2直连/KVM/香港VPS[主机]
- 轻云互联,香港VPS月付22元 美国VPS月付19元美国VPS[主机]
- 个人如何做跨境电商?全球[VPS测评]
- VPSPlayer,超便宜香港cn2线路小带宽VPS,2核4G内存仅30香港VPS[主机]
- UCloud海外节点vps活动:香港、台湾等全球14个机房1折起,最低仅香港VPS[主机]
- 恒星云:香港沙田CN2VPS低至9.9元/起_国内BGP云服务器4核4G香港VPS[主机]
- 岚云:香港安迅/沙田CN2直连,75折起,1核/1G/30G SSD/1香港VPS[主机]
- 2.45亿、成都公安(大数据基座-服务器)采购:神码、虹信、海康、超聚变全球[VPS测评]
- 青云互联:香港vps全场终身8折优惠,CN2线路,提供大带宽vps,月付香港VPS[主机]
- 2022年黄河流域跨境电商博览会将于8月26日至28日在青岛西海岸新区举全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-142751.html











