如果你认真写博客,你肯定会在文章中引用你网站上的其他文章。这时,我们通常使用一个标签来获取它。 Word嵌入功能已添加到WordPress 4.4中,以比较特定显示链接的信息。它可以嵌入任何WordPress网站的WordPress博客中,然后自动转换为相应的内部块。当然,前提是嵌入式博客主题支持Post Embed功能,并且未被禁用。虽然这已经解决了问题,但我们可以有更好的解决方案〜
因为我们正在调用现场文章,如果我们使用 get_posts,我们可以轻松调用文章的元信息,包括浏览量,缩略图,甚至文章摘要(如果有的话)。与可定制的样式相结合,这个内链看起来比普通的A标签高得多。例如以下内容:
我们可以用短代码的方式添加文章 ID 来直接调用文章,非常方便,下面给出实现方法。
下面的代码加入到 functions.php 中:
- /**
- * WordPress通过短代码显示指定文章内容
- * http://www.ilxtx.com/insert-post-through-post-id.html
- */
- function lxtx_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(‘lxtx_fa_insert_post’, ‘lxtx_fa_insert_posts’);
取自《WordPress 文章内链短代码 – Fatesinger》
你可以根据你自己的需要来调整代码,也可以自己自定义 CSS 样式,这里就不给出 CSS 代码了。当然也可以参考本站的结构和 css~
至于调用就非常简单了,直接使用短代码即可;当然,如果你不是在文章内容中,而是在其他地方想调用,则可使用 do_shortcode('')
来调用。