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
- KVMLA→香港 日本 新加坡 VPS 年付 7 折 月付 8 折 内存日本VPS[主机]
- 黑五 CloudCone→三款特价机器 512M 内存年付低至 15 美全球[VPS测评]
- RAKsmart→圣何塞 VPS 年付 19.9 美元起 100M 服务全球[VPS测评]
- 充值活动 hostsolutions→充值立返 50% 最高返 250 全球[VPS测评]
- Bootstrap-datepicker 设置开始时间及结束时间可选范围全球[VPS测评]
- php 随机生成 n 个汉字字符串函数全球[VPS测评]
- MySQL 获取当前时间及时间戳的方法全球[VPS测评]
- hkserversolution→2.3 折优惠 美国原生 IP 解锁“美国VPS[主机]
- RAKsmart→“黑色星期五”购物狂欢 节 I3 低至$46 抢购 G全球[VPS测评]
- DiyVM→69 元 月 XEN-2GB 50GB 2M 香港&洛杉矶香港VPS[主机]
- KhanWebHost→$6.5 月 4 核 3GB 内存 80GB N虚拟空间(主机)
- 酷番云双十一 高防服务器1.5折起 2核2G5M带宽无限流量国内高防只需全球[VPS测评]
- NexusBytes新开分站EntryBytes,1核3G内存/30G 全球[VPS测评]
- 恒创科技 新用户优惠 香港CN2 GIA线路 1G内存 50g硬盘 2M香港VPS[主机]
- 如何让WordPress博客变得更加安全全球[VPS测评]
- 咸鱼云 → 圣何塞 Elite as9929 VPS 季付8折 St全球[VPS测评]
- CubeCloud 2核心 1G内存 15G SSD 1T流量 1G带宽全球[VPS测评]
- 美国信贷市场的痛苦将不亚于上世纪70年代的通胀时期美国VPS[主机]
- 阿里行癫:如何理解当下的云计算全球[VPS测评]
- 快速云:怎么连接vps?VPS的用途有哪些?全球[VPS测评]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
- 硅云服务器拼团活动:香港云服务器1核1G,拼团价仅98元/年香港VPS[主机]
- 拼多多也要做跨境电商?出海之路能一帆风顺吗,了解一下全球[VPS测评]
- Linux chown -R 指令介绍与使用全球[VPS测评]
- 人们为啥不爱吃海底捞了?海底捞上半年亏损达2.97亿 网友神评扎心全球[VPS测评]
- TmhHost优惠码:黑五优惠全场8折,洛杉矶vps、日本CN2、韩国C日本VPS[主机]
- 腾讯云限时秒杀改版:轻量应用服务器2核4G8M仅598元/年,增香港/新香港VPS[主机]
- 阿里云ecs香港服务器怎么购买?阿里云香港2核8G5M云服务器仅2449香港VPS[主机]
- 月神云:洛杉矶CN2 GIA高防vps、香港CN2 GIA VPS全场7香港VPS[主机]
- 快云科技:全场85折,香港CN2云服务器,月付24元起,不满意24小时无香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-142751.html











