WordPress SEO优化 文章内链短代码

WordPress SEO优化 文章内链短代码

如果你认真写博客的话肯定会有在内引用你站内其他的时候。这个时候我们一般都是直接用一个a标签来搞定,虽然这样已经解决了问题,但是我们可以有更好的方案。

因为要的是站内文章,如果我们使用get_posts的话可以很好的调用文章的元信息,包括浏览量,缩略图之类的,甚至文章摘要(如果你有的话)。再加上可以自定义样式,这个看上去可要比普通的A标签高大上多了。比如下面的

我们可以用短代码的方式添加文章ID来直接调用文章,非常方便,下面给出实现方法。

下面的代码加入到functions.php

function fa_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="card-today-history"><div class="card-thContents"><div class="card-thLine"></div><div class="card-thHeroTitle"><a target="_blank" class="label--thTitle" href="' . get_permalink() . '">' . get_the_title() . '</a><div class="v-floatRight card-thMeta">' . get_comments_number(). '<i class="iconfont icon-comment"></i></div></div></div></div>';
    }
    wp_reset_postdata();
    return $content;
}
add_shortcode('fa_insert_post', 'fa_insert_posts');

你可以根据你自己的需要来调整代码,也可以自己自定义CSS样式,我这里就不给出CSS代码了。请无视函数中的css命名,我是直接把历史上的今天的样式直接拿过来了。。

至于调用就非常简单了,直接使用短代码[fa_insert_post ids=123,245]即可

如果你不是在文章内容中,而是在其他地方想调用,则可使用do_shortcode('[fa_insert_post ids=123,245]')来调用。

良好的结构有助于网站SEO,如果对SEO有关注可以好好关注下内链建设。