您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
WordPress短代码添加文章的卡片式链接的教程
云羿谆2023-06-16WordPress教程已有人查阅
导读写博客时,我们经常需要在文章中添加链接。一方面是增加文章的相关性,提高SEO的效果。更重要的是,适当的引用文章也可以使内容更丰富,提高用户体验。

然而,常规的文章链接通常都是直接放在一个链接中,简单而粗糙,但是用户体验不是很好。WordPress在4.4版之后添加了Post Embed,以显示要引用的文章作为嵌入式卡,如下图所示
但是,在实际使用中,主机引用认为这个函数不太实用。除了单一的样式,我们查看源代码,发现它将以js的形式输出iframe框架,这对SEO非常不利,其次,从实际加载效果来看,“卡片”加载效果很差,体验很差!因此,Post嵌入函数在主题中被直接禁用。
首先需要将如下代码放入你主题的函数模板里
functions.php/**
* 卡片式文章内链功能
* https://zhujicankao.com
*/
function yx_embed_posts( $atts, $content = null ){
extract( shortcode_atts( array(
'ids' => ''
),
$atts ) );
global $post;
$content = '';
$postids = explode(',', $ids);
$inset_posts = get_posts(array('post__in'=>$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 ).'"><span class="embed-card-category">'. $category[0]->cat_name .'</span></a>
<span class="embed-card-img">
<a target="_blank" href="' . get_permalink() . '"><img alt="'. get_the_title() . '" src="'. post_thumbnail_src() .'"></a>
</span>
<span class="embed-card-info">
<a target="_blank" href="' . get_permalink() . '">
<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() . '"><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();
return $content;
}
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: 百%;
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,456]
如果你不是在文章内容中,而是在其他地方想调用,则可使用下面代码来调用
do_shortcode('[yx_embed_post ids=123,456]')
适当增加文章链接对SEO有积极的影响,也能使文章更充实,为什么不呢?
本文标签:
很赞哦! ()
相关教程
- (响应式)wordpress模板VieuV4.5主题资讯自媒体博客源码
- (响应式)WordPress主题Ripro9.0博客免扩展二开版
- (自适应多语言)WordPress开源主题MirageV资讯个人博客源码
- (自适应)WordPress主题SEO自媒体博客资讯模板RabbitV2.0
- WordPress主题模板JustNews资讯博客类源码V5.2.2
- (自适应)WordPress二次元博客主题Sakurairo
- 响应式WordPress简约博客主题Alt_Blog
- Wordpress博客新闻主题在线商店平台betheme 21.5.6版
- WordPress个人博客主题 - wp-Concise-v1.0免费下载
- WordPress主题模板主题巴巴/博客X主题源码免费下载
- MYcat实现wordpress库和shopxo库分库
- wordpress、Discuz产品部署示例
图文教程
WordPress可以更换HTML吗
WordPress可以更换HTML吗WordPress可以更换HTML。两种方式,一种是在wordpress后台更改(在后台点击<外观>--><编辑>),直接在原模板文件里改代码。
wordpress常用经典插件99个分享
非常多的插件分类 帮助新手朋友更快了解wordpress插件,当然可能还有许多好的未被找到。 wordpress留言相关插件:1、Akismet:很流行的反垃圾留言插件。可能吧使用WP至今,它已经
WordPress后台删除不需要的侧边栏菜单的方法
聊聊WordPress后台怎么删除不需要的侧边栏菜单,希望对大家有所帮助。自定义后台的侧边栏顶级菜单,首先让我们看看,什么是后台的侧边栏菜单:
wordpress安装手机主题的方法
在此之前,你需要有个wordpress博客平台,然后按照下图的(图示一)安装MobilePress插件。
相关源码
-
(PC+WAP)绿色日志美文文学说说博客网站pbootcms模板除日志博客类网站外,通过替换图文内容可快速适配:心情日记分享平台、文学创作社区、朋友圈内容聚合站、美文鉴赏网站、读书笔记平台等应用场景。查看源码 -
(自适应)绿色农业大型机械设备展示网站模板下载基于PbootCMS内核深度定制开发的农业机械行业专用模板。针对农机设备展示、产品参数说明等需求优化设计,突出农业机械行业特性查看源码 -
(PC+WAP)绿色草坪地坪操场pbootcms网站模板该模板基于PbootCMS内核开发,专为人造草坪、地坪施工企业设计,采用绿色主题呼应行业属性,实现PC与WAP端全栈响应式适配,确保跨设备无缝浏览体验。查看源码 -
(PC+WAP)中英双语户外用品帐篷装备pbootcms网站模板下载这款基于PbootCMS开发的中英文双语模板专为户外装备行业设计,适配PC和移动设备。模板采用现代化设计风格,突出户外产品的功能性和实用性,帮助企业建立专业的国际化展示平台。查看源码 -
(自适应)黑色摄影作品工作室pbootcms模板网站源码下载为风景摄影、个人工作室打造的高端网站模板,基于PbootCMS开源内核开发,采用HTML5自适应架构,PC与移动端实时数据同步,适配各类拍摄作品展示需求。查看源码 -
(自适应响应式)APP应用程序软件介绍落地页源码免费下载该模板为营销技术从业者设计,提供专业的内容展示平台。采用响应式布局,适配软件介绍、APP推广等营销场景,通过可视化后台可快速搭建符合行业特性的展示网站。查看源码
| 分享笔记 (共有 篇笔记) |

