code
pre
你的代码片段
首先 WordPress 本身并没有相关文章的推荐功能,网站之所以需要这样的功能出于两个原因,一方面是推荐相关的内容越优质,访客的留存和继续阅读将会增强,同样从优化角度来说会更加有利于搜索引擎抓取时对页面质量的提升,毕竟一个页面的内容,推荐内容越相关,说明这个页面的质量就越高。
所有废话不多说,下面子凡就把相关的 WordPress 实现相关文章推荐的代码分享出来,供 WordPress 开发者或者喜欢折腾的朋友借鉴参考。
global $post;
$pid = $post->ID;//当前文章的 ID
$tags = [];
$limit = 6;//自定义数量
$post_tags = get_the_terms($pid,'post_tag');
if ( !empty($post_tags) && !is_wp_error($post_tags) ) {
$tags = wp_list_pluck($post_tags, 'term_id');
}
$related_args = [
'ignore_sticky_posts' => 1,
'post_type' => 'post',
'posts_per_page' => $limit,
'post_status' => 'publish',
'post__not_in' => [$pid],
'tag__in' => $tags,
];
$relateds = new WP_Query($related_args);
$post_count = $relateds->post_count;
$output = '';
if($relateds->have_posts()){
while($relateds->have_posts()){
$relateds->the_post();
$output .= '- '.get_the_title().'
';
}
if($post_count<$limit){//数量还不够
wp_reset_postdata();
$limit = $limit - $post_count;//还差几条数据
$pids = wp_list_pluck($relateds->posts,'ID');//获取已有文章 ID
$pids = array_merge([$pid],$pids);//合并文章 ID
$catids = wp_list_pluck(get_the_category($pid),'term_id');//分类 ID
$related_args = [
'ignore_sticky_posts' => 1,
'post_type' => 'post',
'posts_per_page' => $limit,
'post_status' => 'publish',
'post__not_in' => $pids,
'category__in' => $catids,
];
$relateds = new WP_Query($related_args);
$post_count = $relateds->post_count;
if($relateds->have_posts()){
while($relateds->have_posts()){
$relateds->the_post();
$output .= '- '.get_the_title().'
';
}
}
}//数量还不够
if($post_count<$limit){//还不够
wp_reset_postdata();
$limit = $limit - $post_count;//还差数据
$pids = array_merge($pids,wp_list_pluck($relateds->posts,'ID'));//合并文章 ID
$related_args = [
'ignore_sticky_posts' => 1,
'post_type' => 'post',
'posts_per_page' => $limit,
'post_status' => 'publish',
'post__not_in' => $pids,
];
$relateds = new WP_Query($related_args);
if($relateds->have_posts()){
while($relateds->have_posts()){
$relateds->the_post();
$output .= '- '.get_the_title().'
';
}
}
}//还不够
}
wp_reset_postdata();
$output .= '
';
echo $output;
代码看上去很长,但是大家可以简单的看看其实还是很简单的,首先是根据当前文章 ID 获取当前文章设置的标签,根据标签来获取相同标签的文章,并且排除当前文章,如果文章数量不够,就继续或许当前文章分类下面的文章,并且排除当前文章和前面已经获取到的文章,如果还不够我们需要的数量,就整站获取其他文章补够数量。
今天在一为的社区论坛闲逛的时候偶然发现星书签导航有个音乐网站,巧的是之前在Github 找的仿网易云的音乐网站播放不了音乐了,就想来重新搞一个,一顿折腾,也做了,你也可以来试听下:http://music.ixgm.com
可是,你想不想要搭建一个界面现代化的音乐小站呢,SPlayer音乐播放器是imsyy大佬制作的一个项目,接下来带小白们手把手部署这个项目吧!
PS:我自己用的是腾讯云的服务器,没有的自己购买,并且我现在也是使用主流的宝塔面板,docker也是很容易安装成功的,不会安装的自行科普哦,直接上教程了
docker pull imsyy/splayer:latest
docker run -d --name SPlayer -p 25884:25884 imsyy/splayer:latest
PS:这里操作完,需要去左面安全菜单里面去放行对应的端口,25884可以自行更改,
然后就成功了,直接访问 http://localhost:25884 就能开始使用啦!
如果你想用一个二级域名去访问,操作如下,左面菜单-网站-添加站点-保存-域名设置-反向代理-把目标URL:http://localhost:25884更改成$host即可
最近折腾一个网址导航的网站,想在首页弄几个广告位,自己想折腾一下,又没有时间
正好今天逛一为用户中心的时候,老现老六的网站有这么一个广告位源码,就拿来发一下留给自己用,
<!--图片广告区域开始-->
<div style="background-color:#fff;box-shadow:0px 0px 10px #dedede;padding:5px;margin-top:5px;margin-bottom:5px;">
<h3 class="fly-panel-title" style="border-bottom:1px dotted #e2e2e2;margin-bottom:10px;">
赞助商广告 <a style="float:right;color:#dedede;" href="https://www.ixgm.com/" target="_blank"
onmouseover="this.style.color='#F00'" onmouseout="this.style.color='#dedede'">广告招商</a>
</h3>
<a class="adplan" data-id="15" style="display:block;width:100%;margin-bottom:10px;" rel="nofollow" target="_blank"
href="https://www.ixgm.com/" title="广告联系q:1394406|过期时间:永久">
<img src="https://www.xinfangs.com/pic/index/adwei.png"
style="max-height:70px;width:100%;border-radius:4px;" width="644" height="70" title="" align=""
/></a> <!--图片广告区域结束-->
<!--顶部双排广告第二行-->
<style>
#nr {
font-size: 20px;
margin: 0;
background: -webkit-linear-gradient(left,
#ffffff,
#ff0000 6.25%,
#ff7d00 12.5%,
#ffff00 18.75%,
#00ff00 25%,
#00ffff 31.25%,
#0000ff 37.5%,
#ff00ff 43.75%,
#ffff00 50%,
#ff0000 56.25%,
#ff7d00 62.5%,
#ffff00 68.75%,
#00ff00 75%,
#00ffff 81.25%,
#0000ff 87.5%,
#ff00ff 93.75%,
#ffff00 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 200% 100%;
animation: masked-animation 2s infinite linear;
}
@keyframes masked-animation {
0% {
background-position: 0 0;
}
100% {
background-position: -100%, 0;
}
}
</style>
<div
style="background-color:#333;border-radius:25px;box-shadow:0px 0px 5px #f200ff;padding:5px;margin-top:10px;margin-bottom:0px;">
<marquee>
<b id="nr">公告:欢迎来到新方式博客-专注分享科技前沿资讯,体验极致生活的博客!</b> </marquee>
</div>
<!--顶部双排广告第二行-->
<style>
.txtguanggao {
width: 100%;
overflow: hidden;
display: block;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, .05);
}
.txtguanggao a {
width: 24.5%;
float: left;
border-radius: 2px;
line-height: 35.35px;
height: 35.35px;
text-align: center;
font-size: 14px;
color: #fff;
display: inline-block;
background-color: rgb(255, 153, 159);
margin: 2.5px;
transition-duration: .3s;
}
.txtguanggao a:nth-child(1) {
background-color: #dc3545;
}
.txtguanggao a:nth-child(2) {
background-color: #007bff;
}
.txtguanggao a:nth-child(3) {
background-color: #28a745;
}
.txtguanggao a:nth-child(4) {
background-color: #ffc107;
}
.txtguanggao a:nth-child(5) {
background-color: #28a745;
}
.txtguanggao a:nth-child(6) {
background-color: #ffc107;
}
.txtguanggao a:nth-child(7) {
background-color: #dc3545;
}
.txtguanggao a:nth-child(8) {
background-color: #007bff;
}
.txtguanggao a:hover {
background: #FF2805;
color: #FFF
}
@media screen and (max-width: 1000px) {
.txtguanggao a {
width: 47.96%;
float: left;
border-radius: 2px;
line-height: 35.35px;
height: 35.35px;
text-align: center;
font-size: 14px;
color: #fff;
display: inline-block;
background-color: rgb(255, 153, 159);
margin: 2.5px;
transition-duration: .3s;
}
}
</style>
<div class="txtguanggao">
<a href="https://www.ixgm.com/" target="_blank" rel="nofollow"
class="dh">文字广告火爆招租</a> <a href="https://www.ixgm.com/"
target="_blank" rel="nofollow" class="dh">文字广告火爆招租</a> <a
href="https://www.ixgm.com/" target="_blank"
rel="nofollow" class="dh">文字广告火爆招租</a> <a
href="https://www.ixgm.com/" target="_blank"
rel="nofollow" class="dh">文字广告火爆招租</a> <a
href="https://www.ixgm.com/" target="_blank"
rel="nofollow" class="dh">文字广告火爆招租</a> <a
href="https://www.ixgm.com/" target="_blank"
rel="nofollow" class="dh">文字广告火爆招租</a> <a
href="https://www.ixgm.com/" target="_blank"
rel="nofollow" class="dh">文字广告火爆招租</a> <a
href="https://www.ixgm.com/" target="_blank"
rel="nofollow" class="dh">文字广告火爆招租</a>
</div>
</div>
<!--文字广告结束-->
转自:老六资源网
在WordPress中无需插件进行代码压缩和优化网站速度,可以通过以下方法实现:
主题 全局配置文件 中,在functions.php文件中加入以下代码:
有些主题会是在functions-theme.php文件中:
<span class="hljs-comment">/*
WordPress免插件网页代码压缩
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">wp_compress_html</span><span class="hljs-params">()</span></span>{
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">wp_compress_html_main</span> <span class="hljs-params">($buffer)</span></span>{
$initial=strlen($buffer);
$buffer=explode(<span class="hljs-string">"<!--wp-compress-html-->"</span>, $buffer);
$count=count ($buffer);
<span class="hljs-keyword">for</span> ($i = <span class="hljs-number">0</span>; $i <= $count; $i++){
<span class="hljs-keyword">if</span> (stristr($buffer[$i], <span class="hljs-string">'<!--wp-compress-html no compression-->'</span>)) {
$buffer[$i]=(str_replace(<span class="hljs-string">"<!--wp-compress-html no compression-->"</span>, <span class="hljs-string">" "</span>, $buffer[$i]));
} <span class="hljs-keyword">else</span> {
$buffer[$i]=(str_replace(<span class="hljs-string">"\t"</span>, <span class="hljs-string">" "</span>, $buffer[$i]));
$buffer[$i]=(str_replace(<span class="hljs-string">"\n\n"</span>, <span class="hljs-string">"\n"</span>, $buffer[$i]));
$buffer[$i]=(str_replace(<span class="hljs-string">"\n"</span>, <span class="hljs-string">""</span>, $buffer[$i]));
$buffer[$i]=(str_replace(<span class="hljs-string">"\r"</span>, <span class="hljs-string">""</span>, $buffer[$i]));
<span class="hljs-keyword">while</span> (stristr($buffer[$i], <span class="hljs-string">' '</span>)) {
$buffer[$i]=(str_replace(<span class="hljs-string">" "</span>, <span class="hljs-string">" "</span>, $buffer[$i]));
}
}
$buffer_out.=$buffer[$i];
}
$final=strlen($buffer_out);
$savings=($initial-$final)/$initial*<span class="hljs-number">100</span>;
$savings=round($savings, <span class="hljs-number">2</span>);
<span class="hljs-keyword">return</span> $buffer_out;
}
ob_start(<span class="hljs-string">"wp_compress_html_main"</span>);
}
add_action(<span class="hljs-string">'get_header'</span>, <span class="hljs-string">'wp_compress_html'</span>);
然后我们刷新网页就OK了。那么如果我们要一些地方不要压缩,该如何做呢?
<span class="hljs-comment"><!--wp-compress-html--></span><span class="hljs-comment"><!--wp-compress-html no compression--></span>
<span class="hljs-comment"><!--wp-compress-html no compression--></span><span class="hljs-comment"><!--wp-compress-html--></span>
演示站点:
https://ixgm.com/api/pic/top
首先,我们打开服务器的宝塔面板,如果没安装的推荐安装一个,因为这个对建站小白来说非常的方便。
我们参加一个 API 站点
然后到站点设置里申请一个 ssl 认证,再打开强制 https
打开这个站点的目录,创建一个 img.txt 和一个 index.php
我们在 img.txt 中将我们图片的 url 写在里面,一行一个链接
在 index.php 中写下这段代码
<?php
//存有image链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
die('文件不存在');
}
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
$line=trim(fgets($fs));
if($line!=''){
array_push($pics, $line);
}
}
//从数组随机获取链接
$pic = $pics[array_rand($pics)];
//返回指定格式
$type=$_GET['type'];
switch($type){
//JSON返回
case 'json':
header('Content-type:text/json');
die(json_encode(['pic'=>$pic]));
default:
die(header("Location: $pic"));
}
?>
到此大功告成!我们的随机图 API 就做好了!
可以再站点设置中添加防盗链,这样别人就无法使用你的 API 了,防止流量浪费
我们只需要访问这个 API 链接(index.php)就可以立即跳转到我们 img.txt 中的随机一张图片的地址啦!