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[主机]
- 很简单的图片不规则布局样式,纯css样式实现的图片瀑布流布局 全球[VPS测评]
- 拼多多回应筹备跨境电商业务:不予置评 全球[VPS测评]
- Rustrot-深绿色UI电商网站模板HTML5框架 全球[VPS测评]
- 手帕云,便宜小带宽香港CN2 VPS,集群10G防御,双向CN2直连,仅 香港VPS[主机]
- 轻云互联,香港美国vps终身8.8折17元/月起,cn2直连线路,50G 美国VPS[主机]
- GreenCloudVPS:5折、6折优惠,日本、新加坡、香港等KVM 日本VPS[主机]
- 六一云:香港CN2/洛杉矶高防/CDN,“返利+折扣”双优惠,折上折送实 香港VPS[主机]
- 快云科技:2核2G3M香港云BGP/新界CN2云服务器,月付33元 香港VPS[主机]
- 初忆云: 滁州电信、湖北电信云服务器价格低至10元起,续费同价 全球[VPS测评]
- edgeNAT:2021元旦vps促销_全场韩国/美国/香港VPS低至7 韩国VPS[主机]
- edgenat:香港vps、韩国vps、洛杉矶vps买1送1,买多久送多 韩国VPS[主机]
- 华纳云新春活动:香港CN2云服务器4核4G5M仅3476元/3年;美国云 美国VPS[主机]
- 月神科技:香港云服务器,2核2G5M年付200元/年;美国2核2G50M 美国VPS[主机]
- 绮风云:香港云服务器低至9.9元/月起,深圳/北京/内蒙古vps,63. 香港VPS[主机]
- 特网云:香港高防云主机,香港/美国/韩国/大连/沈阳等地区,低至48元/ 韩国VPS[主机]
- dogyun:香港/韩国/美国cn2定制VPS,静态云服务器终身8折,低 韩国VPS[主机]
- MineServer:日本CN2云服务器,2核/2G/15Mbps,年付 日本VPS[主机]
- 零云数据:辽宁BPG云服务器,19元/月起,防50G;香港CN2路线vp 香港VPS[主机]
- 租用美国服务器有哪些安全方案 美国VPS[主机]
- 兼容移动和PC的loading加载和toast消息插件 全球[VPS测评]
- VPS成立BurjeelHoldings 全球[VPS测评]
- 快速云:怎么连接vps?VPS的用途有哪些? 全球[VPS测评]
- 买了一台云服务器到底能干什么? 全球[VPS测评]
- sugarhosts首推DECADE云服务器,香港云服务器,1核512M 香港VPS[主机]
- 恒创科技怎么样?香港美国云服务器/独服/高防全场2.5折起 美国VPS[主机]
- UOVZ怎么样?香港30M大带宽VPS 直连线路月付50元 香港VPS[主机]
- HostKVM香港云地VPS,2核4G内存折后$8.4/月,30M大带宽 香港VPS[主机]
- 青果云怎么样?美国/香港/日本云主机 买多久送多久 日本VPS[主机]
- centos7如何查看firewall防火墙状态:开启/关闭/重启防火墙 全球[VPS测评]
转载请注明原文地址: http://140.238.13.167:12355/read-142751.html











