使用短代码在WordPress文章中创建图文内链

2024-01-05 0 420

正文:

内部链接在搜索引擎优化中扮演着重要的角色,良好的内部链接结构对于优化非常有益。当您在当前页面中引用其他文章或页面时,主动添加内部链接可以增加蜘蛛的抓取数量和深度,并提高收录数量。

使用短代码在WordPress文章中创建图文内链

内部链接通常以文本链接的形式出现。然而,为了提升用户体验,我们可以在内部链接中添加缩略图和内容摘要,使文章更加丰富。在浏览网上文章时,您可能经常会看到插入了缩略图和内容摘要的内部链接。

一、PHP 代码部分

wordpress主题的 functions.php 中加上以下代码:


// 内链图片 src
 function liao_the_thumbnail_src() {
 global $post;
 if ( get_post_meta($post->ID, 'thumbnail', true) ) { //如果有缩略图,则显示缩略图
 $image = get_post_meta($post->ID, 'thumbnail', true);
 return $image;
 } else {
 if ( has_post_thumbnail() ) { //如果有缩略图,则显示缩略图
 $img_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
 return $img_src[0];
 } else {
 $content = $post->post_content;
 preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
 $n = count($strResult[1]);
 if($n > 0){
 return $strResult[1][0]; //没有缩略图就取文章中第一张图片作为缩略图
 }else {
 $random = mt_rand(1, 76);
 return get_template_directory_uri().'/img/random/'. $random .'.jpg'; //文章中没有图片就在 random 文件夹下随机读取图片作为缩略图
 }
 }
 }
 }


//给文章加内链
 function liao_insert_posts( $atts, $content = null ){
 extract( shortcode_atts( array(
 'ids' => ''
 ),
 $atts ) );
 global $post;
 $content = '';
 $postids = explode(',', $ids);
 $inset_posts = get_posts(array('post__in'=>$postids));
 foreach ($inset_posts as $key => $post) {
 setup_postdata( $post );
 $content .= '<div class="neilian"><div class="fl"><a target="_blank" href="' . get_permalink() . '" class="fl"><i class="fa fa-link fa-fw"></i>';
 $content .= get_the_title();
 $content .= '</a><p class="note">';
 $content .= get_the_excerpt();
 $content .= '</p></div><div class="fr"><a target="_blank" href="' . get_permalink() . '"><img src=';
 $content .= liao_the_thumbnail_src();
 $content .= ' class="neilian-thumb"></a></div></div>';
 }
 wp_reset_postdata();
 return $content;
 }
 add_shortcode('neilian', 'liao_insert_posts');

二、CSS 样式


.fl{float:left}
.fr{float:right}
.neilian{margin-bottom:25px;padding:10px;width:100%;height:170px;border:1px solid #e8e8e8;background:#fff;box-shadow:0 1px 0 rgba(0,0,0,.1);cursor:pointer;-webkit-transition:box-shadow 218ms;-moz-transition:box-shadow 218ms;-o-transition:box-shadow 218ms;transition:box-shadow 218ms}
.neilian:hover{box-shadow:0 1px 8px 1px rgba(0,0,0,.1)}
.neilian .fl{width:72%}
.neilian .fr{padding:10px 5px;width:24%}
.neilian .fl a{display:block;margin-right:15px;padding:8px 5px;width:100%;color:#35a56b!important;text-decoration:none;font-size:16px;border:none}
.neilian .fl .note{margin:0 0 5px;padding-left:10px;color:#888;font-size:14px}
.neilian .neilian-thumb{width:170px;height:120px}

三、短代码调用方法

在 文章(post) 内容编辑器 可视化(Visual) 或 文本(text) 状态下,直接使用[code][/code]的格式即可调用。

比如,我要显示 3 个内链文章,就直接写短代码:[code][/code]
如果你需要在文章内容以外的地方调用,可以使用 do_shortcode(”)函数。

四、在后台文本状态下添加快捷按钮

WordPress 默认使用的是 TinyMCE 编辑器。如果你想在文本状态下添加快捷按钮,只需在 functions.php 文件中添加以下代码:


add_action('after_wp_tiny_mce', 'add_button_mce');
function add_button_mce($mce_settings) {
?>
<script type="text/javascript">
 QTags.addButton( 'nl', '文章内链', '', '');
 </script>
<?php
}

转载请注明:汇站网 » 使用短代码在 WordPress 文章中创建图文内链

收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

点赞 (0)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务