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

一般我们从WordPress后台获取的主题中,功能是比较少的,因为优质的主题一般都会需要付费。不过蜗牛也是比较喜欢获取简单的主题,然后加一些功能基本上可以实现基本的网站要求,本身网站是用来做内容的,并不是用来好看和丰富功能花哨的。
一般的主题中没有自带相关内容的功能,我们需要通过插件或者自己修改代码实现,使用插件一般会用到WordPress Related Posts插件,后台可以搜索和下载安装。蜗牛这里搜索到无需插件即可用代码实现的相关文章调用,也是通过TAG关键字匹配信息的。
第一、代码部分
<h3>这几篇文章你可能也喜欢:</h3>
<ul class=\”related_posts\”>
<?php
$post_num = 10;
$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><a rel=\”bookmark\” h=\”<?php the_permalink(); ?>\” title=\”<?php the_title(); ?>\” target=\”_blank\”><?php the_title(); ?></a></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><a rel=\”bookmark\” h=\”<?php the_permalink(); ?>\” title=\”<?php the_title(); ?>\” target=\”_blank\”><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo \'<li>没有相关文章!</li>\’;
?>
</ul>
红色部分数字10表示调用10篇,我们可以根据实际调整。将代码丢到需要的相关文章位置模板中。
第二、调整 样式
我们可以根据实际的页面调整和添加 .related_posts 样式,包括行距等问题。
猜你可能想看的VPS
- 促销 $25 年 2CPU 2G 内存 20G SSD 2.5T 流量 全球[VPS测评]
- 微基主机→深港 IPLC 莞港 IPLC 等转发服务 5Mbps-20M全球[VPS测评]
- 曲娶工工是什么成语?全球[VPS测评]
- HKServerSolution→424 元 月 2 核 2G 内存 2虚拟空间(主机)
- CoalCloud→268 元 月 2 核 2GB 内存 20GB SS虚拟空间(主机)
- 新商家慎重-dogyun→VPS 低至 6.5 折 低至 21 元 月 日本VPS[主机]
- DediPath→美国 E3-1240v2 独立服务器促销 $99 月起独立服务器[U]
- hostsolutions 罗马尼亚 3.6TB 大硬盘 VPS 特惠,全球[VPS测评]
- CloudCone→$15 年 KVM-512MB 10GB 1TB 洛全球[VPS测评]
- WordPress禁用谷歌字体插件:Disable Google Fon全球[VPS测评]
- Google发布nofollow标签新规则,有重大变化全球[VPS测评]
- HostMayo便宜VPS年付30美元起;虚拟主机年付5美元起,可选洛杉全球[VPS测评]
- 【网络星期一】绿云greencloudvps年付25美元特价香港日本新加日本VPS[主机]
- Kimsufi KS3(法国) & KS7(CA) 促销全球[VPS测评]
- 云计算拯救互联网巨头?全球[VPS测评]
- 快速云:怎么连接vps?VPS的用途有哪些?全球[VPS测评]
- 拼多多也要做跨境电商?出海之路能一帆风顺吗,了解一下全球[VPS测评]
- 云计算的三种服务模式,SaaS和PaaS的区别在哪里?全球[VPS测评]
- 互盟云怎么样?1核2G云服务器仅468元/年,香港云主机58元/月香港VPS[主机]
- 二三互联年末回馈:香港/美国云服务器新购特价促销,优惠码可打76折美国VPS[主机]
- 硅云:香港云服务器2核2G5M促销1999元/3年起,香港虚拟主机1G空虚拟空间(主机)
- 亿速云11.11上云狂欢节活动:2核4G国内/香港云服务器,价格999元香港VPS[主机]
- Linux chown -R 指令介绍与使用全球[VPS测评]
- 文物、沉船、村落!干旱导致大量遗迹重见天日 2022年干旱还要持续多久?全球[VPS测评]
- 酷锐云元旦促销活动:香港CN2/美国CN2高防vps月付20元起/美国原美国VPS[主机]
- 中概股扫描网传拼多多正筹备跨境电商平台首站登陆美国美国VPS[主机]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 极光KVM双12:全场月付/季付产品全部5折,永久优惠,年付最高立减15全球[VPS测评]
- 极光kvm怎么样,便宜大带宽香港cmi vps/美国gia vps低至1美国VPS[主机]
- 岚云:香港安迅/沙田CN2直连,75折起,1核/1G/30G SSD/1香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-39773.html











