wordpress美化文章内链采用卡片模式展示推荐文章代码分享

图片[1]-wordpress美化文章内链采用卡片模式展示推荐文章代码分享-轻刻年轮
采用短代码给文章添加卡片式内链,通过get_posts自定义调用文章的标题、发布时间等内容,直接以html的形式在前台加载,在一定程度上提高了用户体验,实际

效果如图:

图片[2]-wordpress美化文章内链采用卡片模式展示推荐文章代码分享-轻刻年轮

演示如下

[yx_embed_post ids=2371]

开始食用啦

将一下代码复制到functions.php中即可。
注:模板不同,有些会是在:(functions-theme.php)只要是全局调用文件就可以啦(例如本站主题就是)

一、将如下代码放入你主题的函数模板里(functions.php)或(functions-theme.php)

精简代码

(去除了红色按钮,将按钮文字颜色进行更改,取消显示评论数量)

/**
* 卡片式文章内链功能
* https://www.ly522.com/2379.html
*/
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 ).'"></a>
<span class="embed-card-img">
</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(), 150, '...' ).'</span>
<span class="card-controls">
<span class="group-data"> <i>时间:</i>'. get_the_time('Y/n/j') .'</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() .'/embed-card.css"/>';
}
wp_reset_postdata();
return $content;
}
add_shortcode('yx_embed_post', 'yx_embed_posts');
css

主题模板目录下新建css文件:embed-card.css 放置以下代码/或者放在主题目录CSS文件下

.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: #5a6a7a;*/
color: #4d5d6e;
}
.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%;
}
}
完整版代码/放置方法同上
<span style="font-size: 14px;">/**</span>
<span style="font-size: 14px;">* 卡片式文章内链功能</span>
<span style="font-size: 14px;">* https://www.ly522.com/2379.html</span>
<span style="font-size: 14px;">*/</span>
<span style="font-size: 14px;">function yx_embed_posts( $atts, $content = null ){</span>
<span style="font-size: 14px;">extract( shortcode_atts( array(</span>
<span style="font-size: 14px;">'ids' =&gt; ''</span>
<span style="font-size: 14px;">),</span>
<span style="font-size: 14px;">$atts ) );</span>
<span style="font-size: 14px;">global $post;</span>
<span style="font-size: 14px;">$content = '';</span>
<span style="font-size: 14px;">$postids = explode(',', $ids);</span>
<span style="font-size: 14px;">$inset_posts = get_posts(array('post__in'=&gt;$postids));</span>
<span style="font-size: 14px;">$category = get_the_category();</span>
<span style="font-size: 14px;">foreach ($inset_posts as $key =&gt; $post) {</span>
<span style="font-size: 14px;">setup_postdata( $post );</span>
<span style="font-size: 14px;">$content .= '&lt;span class="embed-card"&gt;</span>
<span style="font-size: 14px;">&lt;a target="_blank" href="'.get_category_link($category[0]-&gt;term_id ).'"&gt;&lt;/a&gt;</span>
<span style="font-size: 14px;">&lt;span class="embed-card-img"&gt;</span>
<span style="font-size: 14px;">&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;span class="embed-card-info"&gt;</span>
<span style="font-size: 14px;">&lt;a target="_blank" href="' . get_permalink() . '"&gt;</span>
<span style="font-size: 14px;">&lt;span class="card-name"&gt;'. get_the_title() . '&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;/a&gt;</span>
<span style="font-size: 14px;">&lt;span class="card-abstract"&gt;'.wp_trim_words( get_the_excerpt(), 100, '...' ).'&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;span class="card-controls"&gt;</span>
<span style="font-size: 14px;">&lt;span class="group-data"&gt; &lt;i&gt;时间:&lt;/i&gt;'. get_the_time('Y/n/j') .'&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;span class="group-data"&gt; &lt;i&gt;评论:&lt;/i&gt;'. get_comments_number() .'&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;a target="_blank" href="' . get_permalink() . '"&gt;&lt;span class="card-btn-deep"&gt;阅读全文&lt;/span&gt;&lt;/a&gt;</span>
<span style="font-size: 14px;">&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;/span&gt;</span>
<span style="font-size: 14px;">&lt;link rel="stylesheet" href="'. get_template_directory_uri() .'/embed-card.css"/&gt;';</span>
<span style="font-size: 14px;">}</span>
<span style="font-size: 14px;">wp_reset_postdata();</span>
<span style="font-size: 14px;">return $content;</span>
<span style="font-size: 14px;">}</span>
<span style="font-size: 14px;">add_shortcode('yx_embed_post', 'yx_embed_posts');</span>

将下面CSS代码另存为embed-card.css文件,然后放入主题根目录的css文件夹中(与上述PHP代码中/embed-card.css路径对应)

.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&gt;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%;
}
}

调用文章方法

使用的时候只需要在文章里添加短代码即可
单文章/123为文章ID

[yx_emb删除ed_post 删除ids=123]

多文章/ 采用 ,号隔开就好

[yx_embe删除d_post 删除ids=123,234]

如果你不是在文章内容中,而是在其他地方想调用,则可使用下面代码来调用。
【123,234】为文章ID 记得修改

do_short删除code(‘[yx_embe删除d_post 删除ids=123,234]’)

记得把代码中的删除去掉
快速获取文章ID方法
图片[3]-wordpress美化文章内链采用卡片模式展示推荐文章代码分享-轻刻年轮

文章调用word press直接在编辑文章时选择文本在合适位置放置调用代码即可

购买后下载不了源码或源码功能失效请联系小编,确认后可立即退款,请勿无理投诉
© 版权声明
THE END
喜欢就支持一下吧
点赞2赞赏 分享
评论 抢沙发

    暂无评论内容