本站_WordPress技巧分享_纯代码使用WordPress短代码添加文章的卡片式链接

本站:VPS参考评测推荐,专注分享VPS服务器优惠信息!若您是商家也可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
部分文章发布时间较久远,可能存在未知因素,购买时建议在本站搜索商家名称,可查看相关文章充分了解商家!若非中文页面可使用谷歌浏览器同步翻译!PayPal代付/收录合作

写博客时,我们经常需要在文章中添加链接。一方面是增加文章的相关性,提高SEO的效果。更重要的是,适当的引用文章也可以使内容更丰富,提高用户体验。

然而,常规的文章链接通常都是直接放在一个链接中,简单而粗糙,但是用户体验不是很好。WordPress在4.4版之后添加了Post Embed,以显示要引用的文章作为嵌入式卡,如下图所示

但是,在实际使用中,服务器引用认为这个函数不太实用。除了单一的样式,我们查看源代码,发现它将以js的形式输出iframe框架,这对SEO非常不利,其次,从实际加载效果来看,“卡片”加载效果很差,体验很差!因此,Post嵌入函数在主题中被直接禁用。

 

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

/** * 卡片式文章内链功能 * https://本站.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 .= ' '. $category[0]->cat_name .' '. get_the_title() . ' '. get_the_title() . ' '.wp_trim_words( get_the_excerpt(), 100, '...' ).' 时间:'. get_the_time('Y/n/j') .' 人气:'. post_views(false, '', '', false) .' 评论:'. get_comments_number() .' 阅读全文 '; } 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: 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,456]

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

do_shortcode('[yx_embed_post ids=123,456]')

 

适当增加文章链接对SEO有积极的影响,也能使文章更充实,为什么不呢?

本文由本站刊发,转载请注明:本站_WordPress技巧分享_纯代码使用WordPress短代码添加文章的卡片式链接,https://本站.com/1212.html

推荐站内搜索:asp主机、虚拟主机香港主机、海外主机租用、shopex虚拟主机、国内免备案主机、服务器怎么搭建、日本服务器网站、公益免费虚拟主机、已经备案域名、google永久免费的服务器、