wordpress内链文章卡片样式美化

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

添加代码

下载下面的php文件,复制里面的代码添加到主题function.php文件中

/* 卡片式文章内链功能
* https://www.feinews.com
*/
function fn_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($ids);
foreach ($inset_posts as $key => $post) {
setup_postdata( $post );
$content .= '<span class="embed-card">
<a href="'.get_category_link($category[0]-&gt;term_id ).'" target="_blank" rel="noopener"><span class="embed-card-category">'. $category[0]-&gt;cat_name .'</span></a>
<span class="embed-card-img">
<a href="' . get_permalink() . '" target="_blank" rel="noopener"><img src="'.timthumb( pandapro_post_thumbnail_src(), array( 'w' =&gt; '450', 'h' =&gt; '300' ) ).'" alt="'. get_the_title() . '" /></a>
</span>
<span class="embed-card-info">
<a href="' . get_permalink() . '" target="_blank" rel="noopener">
<span class="card-name">'. get_the_title() . '</span>
</a>
<span class="card-abstract">'.wp_trim_words( get_the_excerpt(), 140, '...' ).'</span>
<span class="card-controls">
<span class="group-data"> <i>时间:</i>'. get_the_time('Y/n/j') .'</span>
<span class="group-data"> <i>人气:</i>'. pandapro_post_views(false, '', '', false) .'</span>
<span class="group-data"> <i>评论:</i>'. get_comments_number() .'</span>
<a href="' . get_permalink() . '" target="_blank" rel="noopener"><span class="card-btn-deep">阅读全文</span></a>
</span>
</span>
</span>
';
}
wp_reset_postdata();
return $content;
}
add_shortcode('fn_embed_post', 'fn_embed_posts');

 

feinewsembed.php

注意:上述文件代码中有调用自定义的函数(如阅读量,缩略图等),如果读者使用的话需要根据实际情况进行修改,由于大家的主题不统一,没有办法一一描述。特别是文件里面get_the_post_thumbnail_src 是调用文章缩略图,主题不同,你要根据自己的主题缩略图修改。

添加css样式

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

.embed-card,span.embed-card {
display: block;
position: relative;
width: 95%;
padding: 9px;
margin: 15px auto;
border: 1px solid #d4d4d4;
overflow: hidden;
max-width: 99%;
}
.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%;
}
}

 

使用方法:

使用的时候只需要在文章里添加短代码
[xx_embed_post ids=123,321] xx换成fn
(WordPress文章id自己查看,多篇文章用,隔开)
如果你不是在文章内容中,而是在其他地方想调用,则可使用下面代码来调用。
do_shortcode('[fn_embed_post ids=123,321]')

© 文章系本站作者 飞牛 原创发布于「飞牛士 FeiNews」,文章授权后方可转载,请勿侵权!

(0)

猜你喜欢

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

评论列表(4条)

  • 弥雅
    弥雅 2022-10-20 10:57

    不错哦,很实用。飞牛博主,我可以转载不咯

  • 夏目贵志
    夏目贵志 2021-03-15 14:20

    不错不错 ,就是以后如果换主题 有点麻烦 其他还好~