利用WordPress短代码给文章添加卡片式内链

2018-3-20 17:20 来自本站原创 22,234 40 14
[摘要]

适当增加文章内链对SEO有积极的作用,也让文章更加的丰满,我们通过短代码给文章添加卡片式内链,前端显示效果还不错。

写博客的时候经常需要在文章中添加内链,一方面是为了增加文章关联度,提高SEO效果;更重要的是适当的引用文章,也可以让内容更加丰满,对用户体验上也是有提高;但是常规的文章内链一般就是直接放一个链接进去,简单粗暴但是用户体验不是很好。WordPress在4.4版本后增加了 Post Embed 功能,可以把要引用的文章以嵌入式卡片展现出来,如下图所示:

利用WordPress短代码给文章添加卡片式内链

但是在实际使用中,飞鸟认为这个功能不太实用。除了样式上单一外,我们查看源码发现它会以js的形式来输出一个iframe框架,这对SEO非常不好;其次从实际的加载效果上看,这个“卡片”加载的非常不流畅,体验很差!所以飞鸟直接在主题里禁用了 Post Embed 功能。

WordPress 禁用WordPress的Embed功能 移除加载wp-embed.min.js文件 禁用WordPress的Embed功能 移除加载wp-embed.min.js文件 WordPress在升级至4.4正式版后,增加了Embed功能,对于绝大多数网站来讲,都用不到这个Embed功能,今天就教大家如何禁用WordPress的Embed功能 移除加载wp-embed.mi... 时间:2017/8/5 人气:35012 评论:3 阅读全文

在参考了@龙笑天下@大发等博主的文章后,飞鸟采用短代码给文章添加卡片式内链,通过get_posts自定义调用文章的图片、标题、发布时间等内容,直接以html的形式在前台加载,在一定程度上提高了用户体验,实际效果如下:

WordPress 禁用WordPress的Embed功能 移除加载wp-embed.min.js文件 禁用WordPress的Embed功能 移除加载wp-embed.min.js文件 WordPress在升级至4.4正式版后,增加了Embed功能,对于绝大多数网站来讲,都用不到这个Embed功能,今天就教大家如何禁用WordPress的Embed功能 移除加载wp-embed.mi... 时间:2017/8/5 人气:35012 评论:3 阅读全文

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

/**
* 卡片式文章内链功能 
* https://www.yaxi.net/2018-03-20/1741.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($ids);
 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: 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,245]

(WordPress文章id查看方法不赘述,多篇文章用,隔开)

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

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

适当增加文章内链对SEO有积极的作用,也让文章更加的丰满,何乐而不为呢?

本文最后更新于2020年3月20日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!

如果认为本文对您有所帮助请赞助本站

支付宝扫一扫赞助微信扫一扫赞助

  • 支付宝扫一扫赞助
  • 微信扫一扫赞助
  • 声明:凡注明“本站原创”的所有文字图片等资料,版权均属 雅兮网 所有,欢迎转载,但务请注明出处;
    目前评论:40   其中:访客  0   博主  0
    加载中...
    1. SXJ 1
      2年前 (2021-06-15) 20楼

      大佬,为什么我文章的特色图片会跳出卡片显示啊

    2. 3年前 (2020-12-06) 19楼

      大佬,卡片上下有很多的空白,我想把它靠近点,应该改哪个css呢

    3. 4年前 (2020-03-20) 18楼

      为什么我的最多只能调用5篇文章

      • Wing
        4年前 (2020-03-20)  地下1层

        @才才: 多调用的话会出现什么问题?

        • 才才 1
          4年前 (2020-03-20)  地下2层

          @飞鸟: 没影响,就是调用五个之后就不会显示了,看不到第六个

          • Wing
            4年前 (2020-03-20)  地下3层

            @才才: 我刚测了 没有问题,可以显示7个的,你多个连着使用的时候 记得换行 别紧挨着,如果还不行,可能跟主题样式有关吧

            • 才才 1
              4年前 (2020-03-20)  地下4层

              @飞鸟: 好的,我按你方法试试,谢谢

    4. 记挂 1
      4年前 (2020-02-11) 17楼

      按照方法页面打开空白

      • Wing
        4年前 (2020-02-12)  地下1层

        @记挂: 注意文中代码有自定义函数,如阅读量,缩略图等,如果你的主题没有定义这些函数,肯定是会出错的。

    5. 4年前 (2020-02-09) 16楼

      缩略图左右挤压了,怎么回事啊

      • Wing
        4年前 (2020-02-10)  地下1层

        @佛系软件: 你所说的挤压 是指 图片比例变形了吧?那是正常的,因为这个功能里设置了指定的图片宽高,如果图片原比例与此不同,必然会出现挤压。可以用php裁剪图片来解决

        • 4年前 (2020-02-10)  地下2层

          @飞鸟: 鼠标放上没有阴影,也不会动,应该添加那些CSS啊

    6. 4年前 (2019-10-29) 15楼

      我的主题没有成功

      • Wing
        4年前 (2019-10-30)  地下1层

        @SEO博客: 代码里有自定义函数,阅读量、缩略图 你可以根据你的主题进行修改

    7. alex 1
      5年前 (2019-04-05) 14楼

      有一个问题,貌似通过代码获取的分类名称和链接是当前文章的链接和分类名,有没有办法换成我填ID的那篇文章的分类,这样内容才对应得上

      • Wing
        5年前 (2019-04-06)  地下1层

        @alex: 经你的提醒,发现确实有这个bug,分类名称获取的是当前文章的分类,抽时间重新写一下,到时候再通知你,多谢提醒!

    8. yds 1
      5年前 (2019-01-09) 13楼

      你好,按照要求在functions.php添加,增加了css文件,可是在文章 可视化 添加 短代码[yx_embed_post ids=文章ID],在前台还是显示[yx_embed_post ids=文章ID]这段代码,什么都没有调用到,是怎么回事呢?

      • Wing
        5年前 (2019-01-10)  地下1层

        @yds: 你是不是已经升级到最新WordPress版本了?如果是那可能就是新版本对短代码的不支持了吧,因为我这边还没有升级系统,所以暂无法帮你解答。

        • yds 1
          5年前 (2019-01-11)  地下2层

          @飞鸟: 试了,我是最新版的,可以支持了,但是不知是主题原因还是新版问题,a href="' . get_permalink() . '" rel="nofollow">

          • Wing
            5年前 (2019-01-11)  地下3层

            @yds: 这个问题文中提到了“注意:上述代码中飞鸟有调用自定义的函数(如阅读量,缩略图等),如果读者使用的话需要根据实际情况进行修改,由于大家的主题不统一,飞鸟没有办法一一描述。”因为上文代码中调取图片缩略图是我主题自写的函数,所以其他主题可能不支持,根据自己主题进行修改。

        • Cyber 1
          3年前 (2020-11-16)  地下2层

          @飞鸟: 为什么我主题模版里用the_content()获取文章内容就可以正常显示博客卡
          而用$post->post_content就只显示[yx_embed_post ids=123,245]

    9. Kratos 1
      5年前 (2018-10-22) 12楼

      出现问题:Call to undefined function post_thumbnail_src() 。。。。。

      • Wing
        5年前 (2018-10-22)  地下1层

        @Kratos: post_thumbnail_src() 这个自定义函数,你的主题没有配置。
        “注意:上述代码中飞鸟有调用自定义的函数(如阅读量,缩略图等),如果读者使用的话需要根据实际情况进行修改,由于大家的主题不统一,飞鸟没有办法一一描述。”

    1 2
    发表评论

    疑问 调皮 难过 抠鼻 吓 微笑 可爱 坏笑 惊讶 发呆 疑问 大兵 偷笑 咒骂 发怒 白眼 鼓掌 得意 擦汗 亲亲 大哭 呲牙 晕 强

    分享 40 14 22,234
    Top