600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > WordPress 在文章或评论中内容通过短代码显示在文章或评论中

WordPress 在文章或评论中内容通过短代码显示在文章或评论中

时间:2019-04-20 11:41:53

相关推荐

WordPress 在文章或评论中内容通过短代码显示在文章或评论中

如果你仔细写博客,肯定会在文章中引用你站内的其他文章。这时,我们通常直接使用一个 a 做标签。在 WordPress 4.4 版本新增 Post Embed 功能,可以更具体地显示链接信息,可以在任何地方 WordPress 网站以嵌入的方式插入 WordPress 博客中的文章自动转换为相应的内链块。当然,前提是博客主题的嵌入支持 Post Embed 没有禁用功能。虽然这个问题已经解决了,但我们可以有更好的解决方案~

如果我们使用它,因为我们需要调用站内文章get_posts如果是这样,你可以很好地调用文章的元信息,包括浏览量、缩略图,甚至文章摘要(如果你有的话)。此外,可以定制风格,这个内部链看起来比普通链好 A 标签高多了。

我们可以通过短代码添加文章 ID 直接调用文章非常方便。以下是实现方法。

/*** WordPress在文章或评论中内容通过短代码显示在文章或评论中* /insert-post-through-post-id.html* Modified: -10-22 13:31:49 支持文章超过5篇,支持按id按顺序排列文章*/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, umberposts => count($postids), orderby => post__in, ) ); foreach ($inset_posts as $key => $post) { setup_postdata( $post ); $content .=

. get_the_title() .

. get_comments_number().

; } wp_reset_postdata(); return $content;}add_shortcode(lxtx_fa_insert_post, lxtx_fa_insert_posts);

您可以根据自己的需要调整代码,也可以定制自己 CSS 这里就不给款式了 CSS 代码。当然也可以参考本站的结构和 css~

至于调用,很简单。直接使用短代码[lxtx_fa_insert_post ids=123,245];当然,如果你不想在文章内容中调用,而是想在其他地方调用,可以使用do_shortcode(‘[lxtx_fa_insert_post ids=调用123、245]。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。