| 主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情! |
| 我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作 |
寫博客時,我們經常需要在文章中添加鏈接。一方面是增加文章的相關性,提高SEO的效果。更重要的是,適當的引用文章也可以使內容更豐富,提高用戶體驗。
然而,常規的文章鏈接通常都是直接放在一個鏈接中,簡單而粗糙,但是用戶體驗不是很好。WordPress在4.4版之后添加了Post Embed,以顯示要引用的文章作為嵌入式卡,如下圖所示

但是,在實際使用中,主機引用認為這個函數不太實用。除了單一的樣式,我們查看源代碼,發現它將以js的形式輸出iframe框架,這對SEO非常不利,其次,從實際加載效果來看,“卡片”加載效果很差,體驗很差!因此,Post嵌入函數在主題中被直接禁用。
首先需要將如下代碼放入你主題的函數模板里functions.php
/** * 卡片式文章內鏈功能 * http://m.k2270.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 ).'" rel="external nofollow" ><span class="embed-card-category">'. $category[0]->cat_name .'</span></a> <span class="embed-card-img"> <a target="_blank" href="' . get_permalink() . '" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img alt="'. get_the_title() . '" src="'. post_thumbnail_src() .'"></a> </span> <span class="embed-card-info"> <a target="_blank" href="' . get_permalink() . '" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <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="external nofollow" rel="external nofollow" rel="external nofollow" ><span class="card-btn-deep">閱讀全文</span></a> </span> </span> </span> <link rel="stylesheet" href="'. get_template_directory_uri() .'/css/embed-card.css" rel="external nofollow" />'; } 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有積極的影響,也能使文章更充實,為什么不呢?
這幾篇文章你可能也喜歡:
- Lisa主持人的新AMD EPYC新加坡本地IP VP現已開始銷售,解鎖了所有Tiktok和Shopee,并首先享受折扣代碼!
- DMIT洛杉磯CMIN2特殊VPS重新提示,移動用戶的年度付款僅為49.9美元
- UCloud Global Spring促銷:在2025年實現性能飛躍,并在GPU云服務器和云主機上享有巨大折扣!
- Akkocloud:全球高質量的CN2 GIA服務器和獨立服務器解決方案
- HOSTKVM韓國本地VPS在有限的時間內可享受10%的折扣,您可以享受2核4G配置,價格為11.7美元,適合國際業務擴張(VPS品牌在韓國都優越)
本文由主機參考刊發,轉載請注明:主機參考_WordPress技巧分享_純代碼使用WordPress短代碼添加文章的卡片式鏈接 http://m.k2270.com/1212.html
主機參考

















主機參考_WordPress技巧分享_純代碼使用WordPress短代碼添加文章的卡片式鏈接