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
- DMIT→洛杉矶 CN2 GIA VPS 1GB 内存 不限流量 10M全球[VPS测评]
- PHP 国家及地区代码数组全球[VPS测评]
- 手字下面有个天是什么成语?全球[VPS测评]
- [5.1]DediPath 全场 VPS 四折 洛杉矶 1Gbps 不限独立服务器[U]
- 非常便宜 centerhop→1 核 768M 15G SSD 1T 流全球[VPS测评]
- 便宜 VPS 21 刀 年 512M 内存 100M 宽带不限量 hos全球[VPS测评]
- 促销 TmhHost→香港独立服务器 电信双向 CN2 联通移动直连 月独立服务器[U]
- 腾讯云秒杀→个人上海云服务器年付 99 元起 企业用户 2C4G5M 三全球[VPS测评]
- TripodCloud→圣何塞 CN2 GIA 线路 KVM 年付 38全球[VPS测评]
- DogYun 德国(CN2) 香港(CMI)上线 支持小时计费 自助换 香港VPS[主机]
- 促销 HostKvm→香港云地国际 新加坡套餐限量 7 折促销 月付 5香港VPS[主机]
- uuuvps→建站精品 VPS 香港 cn2+日本 cn2+美国 cn2日本VPS[主机]
- WordPress博客固定链接是否会影响网站SEO?全球[VPS测评]
- 更新WordPress网站PHP7.2新版本的注意事项全球[VPS测评]
- PIGYun香港/韩国/美国高端优化线路,优惠后最低月付14元起,可选香韩国VPS[主机]
- Lean 大神 OpenWRT 软路由固件精简版编译教程全球[VPS测评]
- WordPress文章标题如何命名?SEO优化这样做全球[VPS测评]
- 咸鱼科技(Saltyfish)美国费里蒙 圣何塞CN2 GIA → 促销美国VPS[主机]
- Mysql连接数过高,如何检查?全球[VPS测评]
- 什么是Bootstrap?全球[VPS测评]
- 云服务器和服务器各有什么优缺点?全球[VPS测评]
- 腾讯会议故障了 提示:会议发生异常请重新加入会议全球[VPS测评]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- 香港云服务器对于建站有什么便捷的地方?香港VPS[主机]
- Hostkvm:香港/韩国CN2云服务器 大带宽VPS 2核4G,$7.韩国VPS[主机]
- 2023年 IDC市场结构和方向的深度分析全球[VPS测评]
- 衡天云:香港/美国物理服务器499元/月_香港/美国云服务器_超低特价美国VPS[主机]
- 买了一台云服务器到底能干什么?全球[VPS测评]
- 好朋友51WORLD启动“地球克隆计划5”,我要去元宇宙参加了全球[VPS测评]
- 千古互动:香港vps,1核/1G/20M,月付35元起;挂机宝5元/月起香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-39750.html











