正务库

wordpress给文章加内链或外链 短代码

时间:2年前   游览量:332    标签: nbsp    quot    39    gt    lt    contents   

wordpress文章编辑框内,文本模式下,出现2个段代码框,根据自己的需求使用代码吧,将代码插入到 主题目录的 functions.php 内即可!

  1. //给文章加内链短代码

  2. function git_insert_posts($atts$content = null)

  3. {

  4.     extract(shortcode_atts(array('ids' => ''), $atts));

  5.     global $post;

  6.     $content = '';

  7.     $postids = explode(',', $ids);

  8.     $inset_posts = get_posts(array('post__in' => $postids));

  9.     foreach ($inset_posts as $key => $post) {

  10.         setup_postdata($post);

  11.         $content .= '<div class="neilian"><div class="fll"><a target="_blank" href="' . get_permalink() . '" class="fll linkss"><i class="fa fa-link fa-fw"></i>  ';

  12.         $content .= get_the_title();

  13.         $content .= '</a><p class="note">';

  14.         $content .= get_the_excerpt();

  15.         $content .= '</p></div><div class="frr"><a target="_blank" href="' . get_permalink() . '"><img src=';

  16.         $content .= link_the_thumbnail_src();

  17.         $content .= ' class="neilian-thumb"></a></div></div>';

  18.     }

  19.     wp_reset_postdata();

  20.     return $content;

  21. }

  22. add_shortcode('neilian', 'git_insert_posts');


  23. //给文章加外链短代码

  24. function git_external_posts($atts$content = null)

  25. {

  26.     extract(shortcode_atts(array('img' => '0'), $atts));

  27.     $ch = curl_init( $content );

  28.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  29.     $result = curl_exec($ch);

  30.     curl_close($ch);

  31.     $title = preg_match('!<title>(.*?)</title>!i', $result$matches) ? $matches[1] : '因为某些不可控制原因,标题已丢失,请勿想念';

  32.     $tags = get_meta_tags( $content );

  33.     $description = $tags['description'];

  34.     if$img ==0){

  35.     $imgpath = get_template_directory_uri() . '/assets/img/pic/' . mt_rand(1, 12) . '.jpg';}if$img ==1){

  36.     $imgpath = '//image.thum.io/get/width/160/' . $content . '';}

  37.     global $post;

  38.     $contents = '';

  39.         setup_postdata($post);

  40.         $contents .= '<div class="neilian wailian"><div class="fll"><a target="_blank" href="' . $content . '" class="fll linkss"><i class="fa fa-link fa-fw"></i>  ';

  41.         $contents .= $title;

  42.         $contents .= '</a><p class="note">';

  43.         $contents .= $description;

  44.         $contents .= '</p></div><div class="frr"><a target="_blank" href="' . $content . '"><img src=';

  45.         $contents .= $imgpath;

  46.         $contents .= ' class="neilian-thumb"></a></div></div>';

  47.     wp_reset_postdata();

  48.     return $contents;

  49. }if ( function_exists('curl_init') ) {

  50. add_shortcode('wailian', 'git_external_posts');

  51. }