首发于 Ardye's Life
wordpress内链文章卡片样式显示

wordpress内链文章卡片样式显示

背景

博客文章应用站点内的文章链接时,常规就是放一个链接地址就行,既简单又粗暴但是用户体验感不是很好。

要是能像知乎文章显示那样,链接自动转化成卡片式链接就美观多了,一方面提高了文章关联度,另一方面提高了网站SEO效果。

效果如下

实现方法

采用短代码给文章添加卡片式内链,通过get_posts自定义调用文章的图片、标题、发布时间等内容,直接以html的形式在前台加载

function.php 添加代码

/**
* 卡片式文章内链功能
*/
function yx_embed_posts( atts,content = null ){
extract( shortcode_atts( array(
'ids' => ''
),
atts ) );
globalpost;
content = '';postids = explode(',', ids);inset_posts = get_posts(array('post__in'=>postids));category = get_the_category(ids);
foreach (inset_posts as key =>post) {
setup_postdata( post );content .= '<span class="embed-card">
<a target="_blank" rel="noopener noreferrer">term_id ).'"><span class="embed-card-category">'. category[0]->cat_name .'</span></a>
<span class="embed-card-img">
<a target="_blank" href="' . get_permalink() . '" rel="noopener noreferrer"><img alt="'. get_the_title() . '" src="'. post_thumbnail_src() .'"></a>
</span>
<span class="embed-card-info">
<a target="_blank" href="' . get_permalink() . '" rel="noopener noreferrer">
<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="' . get_permalink() . '" rel="noopener noreferrer"><span class="card-btn-deep">阅读全文</span></a>
</span>
</span>
</span>
<link rel="stylesheet" href="'. get_template_directory_uri() .'/css/embed-card.css"/>';
}
wp_reset_postdata();
returncontent;
}
add_shortcode('yx_embed_post', 'yx_embed_posts');

添加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%;
}
}

短代码调用

{yx_embed_post ids=123,245]

其中123,245是文章的后台id号;

如果你不是在文章内容中,而是在其他地方想调用,则可使用下面代码来调用。

do_shortcode('{yx_embed_post ids=123,245]')

将'{'符号改成'['

注意事项

function.php里添加的调用函数(缩略图、阅读量、人气、评论等)需要根据主题自定义添加和删除的,不然网站可能会报错;相关代码位置如下:



这个是修改后的代码截图:


原文链接:

深圳SEO优化公司大鹏模板推广推荐宁德关键词按天收费报价荆州外贸网站建设报价蚌埠网站建设设计哪家好吉林百度网站优化排名江门模板网站建设大丰英文网站建设哪家好河源外贸网站建设公司德州网站设计报价南澳网站优化按天收费推荐成都网络广告推广多少钱光明网站制作新乡建设网站多少钱淮北网站搜索优化推荐揭阳至尊标王推荐通化seo排名推荐伊春网络广告推广拉萨至尊标王哪家好九江建网站公司河池网站排名优化价格阜阳至尊标王通辽百度竞价鞍山企业网站改版推荐武汉seo网站优化公司文山百度竞价包年推广多少钱长治品牌网站设计推荐迪庆外贸网站建设公司广州英文网站建设价格桐城SEO按效果付费绵阳网站设计公司歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化