服务器与VPS
促销优惠信息

建站技巧:给文章超链接添加像知乎一样的卡片式链接

最近看到知乎的超链接显示为卡片式链接感到很美观,我觉得自己建站时应该可以用的到。所以尝试着看看有没有人分享这个代码,功夫不负有心人,还真被我发现了:

一、网站链接仿知乎生成卡片链接:

1、引用js代码:

在网站的head或者foot文件中加入以下js引用代码。

<script src="https://cdn.jsdelivr.net/gh/Sanarous/files@1.13/js/linkcard.js"></script>

2、也可以保存为自己的的js文件如:linkcard.js,然后上传到自己的云服务器空间,引用时改为自己的js地址。js内容如下:

window.onload=function(){
		var LinkCards=document.getElementsByClassName('LinkCard');
		if(LinkCards.length != 0){
		var LinkCard=LinkCards[0];
		var link=LinkCard.href;
		var title=LinkCard.innerText;
		LinkCard.innerHTML="<style type=text/css>.LinkCard,.LinkCard:hover{text-decoration:none;border:none!important;color:inherit!important}.LinkCard{position:relative;display:block;margin:1em auto;width:390px;box-sizing:border-box;border-radius:12px;max-width:100%;overflow:hidden;color:inherit;text-decoration:none}.ztext{word-break:break-word;line-height:1.6}.LinkCard-backdrop{position:absolute;top:0;left:0;right:0;bottom:0;background-repeat:no-repeat;-webkit-filter:blur(20px);filter:blur(20px);background-size:cover;background-position:center}.LinkCard,.LinkCard:hover{text-decoration:none;border:none!important;color:inherit!important}.LinkCard-content{position:relative;display:flex;align-items:center;justify-content:space-between;padding:12px;border-radius:inherit;background-color:rgba(246,246,246,0.88)}.LinkCard-text{overflow:hidden}.LinkCard-title{display:-webkit-box;-webkit-line-clamp:2;overflow:hidden;text-overflow:ellipsis;max-height:calc(16px * 1.25 * 2);font-size:16px;font-weight:500;line-height:1.25;color:#1a1a1a}.LinkCard-meta{display:flex;margin-top:4px;font-size:14px;line-height:20px;color:#999;white-space:nowrap}.LinkCard-imageCell{margin-left:8px;border-radius:6px}.LinkCard-image{display:block;width:60px;height:auto;border-radius:inherit}</style><span class=LinkCard-backdrop style=background-image:url(https://zhstatic.zhihu.com/assets/zhihu/editor/zhihu-card-default.svg)></span><span class=LinkCard-content><span class=LinkCard-text><span class=LinkCard-title>"+title+"</span><span class=LinkCard-meta><span style=display:inline-flex;align-items:center>​<svg class="+"'Zi Zi--InsertLink'"+" fill=currentColor viewBox="+"'0 0 24 24'"+" width=17 height=17><path d="+"'M6.77 17.23c-.905-.904-.94-2.333-.08-3.193l3.059-3.06-1.192-1.19-3.059 3.058c-1.489 1.489-1.427 3.954.138 5.519s4.03 1.627 5.519.138l3.059-3.059-1.192-1.192-3.059 3.06c-.86.86-2.289.824-3.193-.08zm3.016-8.673l1.192 1.192 3.059-3.06c.86-.86 2.289-.824 3.193.08.905.905.94 2.334.08 3.194l-3.059 3.06 1.192 1.19 3.059-3.058c1.489-1.489 1.427-3.954-.138-5.519s-4.03-1.627-5.519-.138L9.786 8.557zm-1.023 6.68c.33.33.863.343 1.177.029l5.34-5.34c.314-.314.3-.846-.03-1.176-.33-.33-.862-.344-1.176-.03l-5.34 5.34c-.314.314-.3.846.03 1.177z'"+" fill-rule=evenodd></path></svg></span>"+link+"</span></span><span class=LinkCard-imageCell><img class=LinkCard-image alt=图标 src=https://site-1258928558.cos.ap-guangzhou.myqcloud.com/linkcard.png></span></span>";

		for (var i = LinkCards.length - 1; i >= 1; i--) {
		LinkCard=LinkCards[i];
		title=LinkCard.innerText;
		link=LinkCard.href;
		LinkCard.innerHTML="<span class=LinkCard-backdrop style=background-image:url(https://zhstatic.zhihu.com/assets/zhihu/editor/zhihu-card-default.svg)></span><span class=LinkCard-content><span class=LinkCard-text><span class=LinkCard-title>"+title+"</span><span class=LinkCard-meta><span style=display:inline-flex;align-items:center>​<svg class="+"'Zi Zi--InsertLink'"+" fill=currentColor viewBox="+"'0 0 24 24'"+" width=17 height=17><path d="+"'M6.77 17.23c-.905-.904-.94-2.333-.08-3.193l3.059-3.06-1.192-1.19-3.059 3.058c-1.489 1.489-1.427 3.954.138 5.519s4.03 1.627 5.519.138l3.059-3.059-1.192-1.192-3.059 3.06c-.86.86-2.289.824-3.193-.08zm3.016-8.673l1.192 1.192 3.059-3.06c.86-.86 2.289-.824 3.193.08.905.905.94 2.334.08 3.194l-3.059 3.06 1.192 1.19 3.059-3.058c1.489-1.489 1.427-3.954-.138-5.519s-4.03-1.627-5.519-.138L9.786 8.557zm-1.023 6.68c.33.33.863.343 1.177.029l5.34-5.34c.314-.314.3-.846-.03-1.176-.33-.33-.862-.344-1.176-.03l-5.34 5.34c-.314.314-.3.846.03 1.177z'"+" fill-rule=evenodd></path></svg></span>"+link+"</span></span><span class=LinkCard-imageCell><img class=LinkCard-image alt=图标 src=https://site-1258928558.cos.ap-guangzhou.myqcloud.com/linkcard.png></span></span>";
		}
	}
}

3、写文章时超链接代码中加入class,如:

<a href="https://chukuangren.com" class="LinkCard">楚狂人</a>

一定要注意 LinkCard 的大小写不能错!

此方法来自:https://bestzuo.cn/posts/3858317073.html

二、wordpress纯代码生成卡片式链接:

首先将如下代码放入你主题的函数模板里functions.php

/**
* 卡片式文章内链功能
* https://zhujicankao.com
*/
function yx_embed_posts( $atts, $content = null ){
extract( shortcode_atts( array(
'ids' =&gt; ''
),
$atts ) );
global $post;
$content = '';
$postids = explode(',', $ids);
$inset_posts = get_posts(array('post__in'=&gt;$postids));
$category = get_the_category();
foreach ($inset_posts as $key => $post) {
setup_postdata( $post );
$content .= '<span class="embed-card">
<a target="_blank" href="'.get_category_link($category[0]->term_id%20).'"><span class="embed-card-category">'. $category[0]->cat_name .'</span></a>
<span class="embed-card-img">
<a target="_blank" href="'%20.%20get_permalink()%20.%20'"><img alt="'. get_the_title() . '" src="'.%20post_thumbnail_src()%20.'"></a>
</span>
<span class="embed-card-info">
<a target="_blank" href="'%20.%20get_permalink()%20.%20'">
<span class="card-name">'. get_the_title() . '</span>
</a>
<span class="card-abstract">'.wp_trim_words( get_the_excerpt(), 100, '...' ).'</span>
<span class="card-controls">
<span class="group-data"> <i>时间:</i>'. get_the_time('Y/n/j') .'</span>
<span class="group-data"> <i>人气:</i>'. post_views(false, '', '', false) .'</span>
<span class="group-data"> <i>评论:</i>'. get_comments_number() .'</span>
<a target="_blank" href="'%20.%20get_permalink()%20.%20'"><span class="card-btn-deep">阅读全文</span></a>
</span>
</span>
</span>
<link rel="stylesheet" href="'.%20get_template_directory_uri()%20.'/css/embed-card.css"/>';
}
wp_reset_postdata();
return $content;
}
add_shortcode('yx_embed_post', 'yx_embed_posts');

2、CSS代码,建议另存为embed-card.css并放入主题根目录的css文件夹中(与上述PHP代码中路径对应)

.embed-card,span.embed-card {
display: block;
position: relative;
width: 620px;
padding: 9px;
margin: 30px auto;
border: 1px dashed #d4d4d4;
overflow: hidden;
max-width: 90%;
}
.embed-card:hover,span.embed-card:hover {
box-shadow: 1px 1px 8px #eee;
}
.embed-card a,span.embed-card a {
padding-right: 0;
text-decoration: none;
color: #313131;
}
.embed-card span,span.embed-card span {
display: block;
padding-right: 0;
}
.embed-card-category {
display: inline-block;
height: 20px;
line-height: 20px;
padding: 0 5px;
font-size: 12px;
}
.embed-card-category {
background-color: #6a99d8;
background-color: rgba(43,110,200,0.8);
color: #fff;
}
.embed-card-category:hover {
background-color: #d5e2f4;
background-color: rgba(43,110,200,1);
}
.embed-card .embed-card-category {
position: absolute;
top: 9px;
left: 0;
padding-right: 5px;
}
.embed-card-img {
float: left;
margin-right: 14px;
}
.embed-card-img img {
width: 180px;
height: 150px;
}
.embed-card-info {
padding-right: 4px;
overflow: hidden;
}
.embed-card-info .card-name {
font-size: 16px;
height: 44px;
line-height: 22px;
margin-bottom: 10px;
margin-top: 7px;
overflow: hidden;
text-overflow: ellipsis;
white-space: normal;
font-weight: bold;
}
.embed-card-info .card-tags {
height: 20px;
overflow: hidden;
}
.embed-card-info .card-tags>span {
display: inline-block;
padding: 0 7px;
margin-right: 8px;
height: 16px;
border: 1px solid #eee;
line-height: 16px;
color: #999;
font-size: 12px;
}
.embed-card-info .card-tags span.tag-noborder {
border: 0;
}
.embed-card-info .card-abstract {
height: 36px;
line-height: 18px;
margin: 5px 0;
font-size: 12px;
color: #666;
overflow: hidden;
margin-bottom: 20px;
}
.embed-card-info .card-controls {
overflow: hidden;
line-height: 28px;
}
.embed-card-info .card-controls .group-data {
float: left;
margin-right: 10px;
color: #999;
font-size: 12px;
}
.embed-card-info .card-controls .group-data i {
margin-right: 5px;
font-style: normal!important;
}
.embed-card-info .card-btn-deep {
float: right;
width: 68px;
height: 28px;
margin-left: 10px;
line-height: 28px;
text-align: center;
font-size: 12px;
background-color: #ff5e5c;
color: #fff;
}
.embed-card-info .card-btn-deep:hover {
opacity: .9;
}
@media only screen and (max-width:700px) {
span.embed-card {
width: 95%;
padding-left: 0;
padding-right: 0;
}
.embed-card .embed-card-img {
width: 24.27184%;
margin-left: 9px;
}
.embed-card .embed-card-img img {
width: 100%;
height: auto;
}
.embed-card .embed-card-info {
overflow: visible;
padding: 0 9px;
}
.embed-card .embed-card-info .card-name {
margin-top: 1%;
margin-bottom: 1.5%;
}
}

3、使用:
使用的时候只需要在文章里添加短代码:

[yx_embed_post ids=123,456]

楚狂人 » 建站技巧:给文章超链接添加像知乎一样的卡片式链接

相关推荐

  • 暂无文章

评论 抢沙发

  • (必填)
  • (必填)

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏