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
- 2019 年 2 月 Vultr 最新促销活动整理 新注册用户完成首充最全球[VPS测评]
- 水墨云五一促销五折起,香港 cn2 美国 cn2 线路 vps 低至¥2美国VPS[主机]
- 桔子 VPS→5 折促销 三网直连(回程 CN2)+ddos 防御 美国美国VPS[主机]
- MoeCloud 香港 HKT VDS 特别促销,G 口家宽无限流量,香香港VPS[主机]
- 黑色星期五 hostsolutions→9 欧 年 1G 内存 10T 全球[VPS测评]
- Hostigger→$11.94 年 1GB 内存 10GB SSD 空虚拟空间(主机)
- 大盘机 $3.78 月 1G 内存 200G 硬盘 3T 流量 1Gbp全球[VPS测评]
- 补货 搬瓦工→DC6 机房 CN2 GIA 线路补货了 2.5Gbps 全球[VPS测评]
- OneVPS→$3.75 月 512MB 内存 20GB SSD 空间 虚拟空间(主机)
- VPS服务器开源 PHP 探针X 探针(刘海探针)介绍全球[VPS测评]
- PacificRack 便宜 VPS 512m内存 10G纯ssd 1T全球[VPS测评]
- WordPress基于WPJAM BASIC开发的免费主题:Autumn全球[VPS测评]
- WordPress5.2发布要求服务器配置不低于PHP5.6.20与My全球[VPS测评]
- 酷锐云双十一 香港电信GIA 月付最低32元 美国CERA GIA 20美国VPS[主机]
- 不要随意安装来路不明的破解WordPress主题全球[VPS测评]
- 搬瓦工VPS,主机界高端线路中的战斗机,提供四条超大带宽高端线路全球[VPS测评]
- Muvhost 美国达拉斯VPS 测试(10Gbps)美国VPS[主机]
- 云计算到底是谁发明的?全球[VPS测评]
- 自适应bootstrap左侧导航可关闭全球[VPS测评]
- 什么是linux面板?常见的Linux控制面板有哪些?全球[VPS测评]
- 因搜包耽误员工下班,苹果将支付3050万美元赔款全球[VPS测评]
- 亿速云“双11”上云狂欢大促 高防云服务器、裸金属服务器优惠套餐全球[VPS测评]
- VPSPlayer,超便宜香港cn2线路小带宽VPS,2核4G内存仅30香港VPS[主机]
- 八点云主机怎么样?香港2核4G5M带宽云主机,98元/月;国内4核8G云香港VPS[主机]
- 快速云:科普云服务器服务器的概念和作用2022-08-2313:26来源全球[VPS测评]
- 腾讯云免备案海外服务器:高速网络免备案,助力企业出海!全球[VPS测评]
- 长三角地区的算力需求规模到底有多大?全球[VPS测评]
- 快云科技:香港CN2VPS,2核/2GB内存/3M独享,仅售14.98元香港VPS[主机]
- 云服务器和云服务器租用,云服务器做什么??全球[VPS测评]
- Vmshell:香港cmi vps,512MB内存/10GB SSD空间虚拟空间(主机)
转载请注明原文地址:http://140.238.13.167:12355/read-142751.html











