13071077710
您的当前位置: 首页 > wordpress > wordpress插入指定ID的文章

wordpress插入指定ID的文章

时间: 2017-11-22

后台效果图:

前台这样显示:

代码

//插入指定文章
add_action('media_buttons_context', 'mee_insert_post_custom_button');
function mee_insert_post_custom_button($_var_13)
{
$_var_13 .= '<button type="button" id="insert-media-button" class="button insert-post-embed" data-editor="content"><span class="dashicons dashicons-pressthis"></span>插入指定文章</button><div class="smilies-wrap"></div><script>jQuery(document).ready(function(){jQuery(document).on("click", ".insert-post-embed",function(){var post_id=prompt("输入文章ID,多个文章,使用英文逗号隔开","");if (post_id!=null && post_id!=""){send_to_editor("[zixieme_insert_post ids="+ post_id +"]");}return false;});});</script>';
return $_var_13;
}

function mee_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="warp-post-embed"><a href="' . get_permalink() . '" target="_blank" ><div class="embed-bg" style="background-image:url('.Zixie_image().')"></div><div class="embed-content"><h2>'.get_the_title().'</h2><p>'. wp_trim_words(get_the_excerpt(),50 ) .'</p></div></a></div>';
}
wp_reset_postdata();
return $content;
}
add_shortcode('zixieme_insert_post', 'mee_insert_posts');

也可以设置添加带有缩略图的文章,如上边代码style="background-image:url('.Zixie_image().')"就是获取文章的缩略图。如何获取文章图片?点击查看

Copyright © 2013-2019 子歇    版权所有   网站地图