[wm_red]介绍[/wm_red]
在WordPress的编辑器当中有一个超链接按钮,可以实现在其中搜索文章然后插入即可,但是有一点不好的是,这种插入的方法文章只有一个标题,并且改标题也仅仅是一个超链接文本的形式存在,如果当想要在这个自动插入的内链文本中自定义文本的语义化的时候就需要手动去设置文本的html语义化。
西瓜君今天在网络上突然发现一段神奇的代码,可以实现将文章ID插入到文章当中自动获取文章标题以及图片还有描述等信息。
代码
//给文章加内链短代码
function gdk_insert_temp($atts, $content = null) {
extract(shortcode_atts(array( 'id' => '' ) , $atts));
$data = get_post($id);
$content = $data->post_content;
return $content;
}
add_shortcode('temp', 'gdk_insert_temp');
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');
[wm_red]使用方法[/wm_red]
将以上代码粘贴到您主题的function.php文件中的最后一行就可以了。
至于调用就非常简单了,直接使用短代码[fa_insert_post ids=123,245]即可,如果你不是在文章内容中,而是在其他地方想调用,则可使用do_shortcode('[fa_insert_post ids=123,245]')来调用。