在wordpress文章编辑框内,文本模式下,出现2个段代码框,根据自己的需求使用代码吧,将代码插入到 主题目录的 functions.php 内即可!
- //给文章加内链短代码
- function git_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="neilian"><div class="fll"><a target="_blank" href="' . get_permalink() . '" class="fll linkss"><i class="fa fa-link fa-fw"></i> ';
- $content .= get_the_title();
- $content .= '</a><p class="note">';
- $content .= get_the_excerpt();
- $content .= '</p></div><div class="frr"><a target="_blank" href="' . get_permalink() . '"><img src=';
- $content .= link_the_thumbnail_src();
- $content .= ' class="neilian-thumb"></a></div></div>';
- }
- wp_reset_postdata();
- return $content;
- }
- add_shortcode('neilian', 'git_insert_posts');
- //给文章加外链短代码
- function git_external_posts($atts, $content = null)
- {
- extract(shortcode_atts(array('img' => '0'), $atts));
- $ch = curl_init( $content );
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($ch);
- curl_close($ch);
- $title = preg_match('!<title>(.*?)</title>!i', $result, $matches) ? $matches[1] : '因为某些不可控制原因,标题已丢失,请勿想念';
- $tags = get_meta_tags( $content );
- $description = $tags['description'];
- if( $img ==0){
- $imgpath = get_template_directory_uri() . '/assets/img/pic/' . mt_rand(1, 12) . '.jpg';}if( $img ==1){
- $imgpath = '//image.thum.io/get/width/160/' . $content . '';}
- global $post;
- $contents = '';
- setup_postdata($post);
- $contents .= '<div class="neilian wailian"><div class="fll"><a target="_blank" href="' . $content . '" class="fll linkss"><i class="fa fa-link fa-fw"></i> ';
- $contents .= $title;
- $contents .= '</a><p class="note">';
- $contents .= $description;
- $contents .= '</p></div><div class="frr"><a target="_blank" href="' . $content . '"><img src=';
- $contents .= $imgpath;
- $contents .= ' class="neilian-thumb"></a></div></div>';
- wp_reset_postdata();
- return $contents;
- }if ( function_exists('curl_init') ) {
- add_shortcode('wailian', 'git_external_posts');
- }