最新消息:

WordPress 文章外链特色图片

很多小伙伴们觉得自己所使用的主题在特色图(缩略图)这方面做得不够强大,比如说有的文章不想用本地的图片作为文章的特色图,还比如说想自动用文章中的的图片作为特色图。那么现在就来跟大家分享一下如何解决这些问题,将下列代码覆盖你所使用的主题的有关特色图的代码(一般在 functions.php 中):

/*
 功能描述: 文章特色图片
 功能介绍页面: https://www.ivicos.com/86.html
*/

$postmeta_thumblink = array(
 array(
 "name" => "thumblink",
 "std" => ""
 )
);

function hui_postmeta_thumblink() {
 global $post, $postmeta_thumblink;
 foreach($postmeta_thumblink as $meta_box) {
 $meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
 if($meta_box_value == "")
 $meta_box_value = $meta_box['std'];
 echo'<p>'.(isset($meta_box['title']) ? $meta_box['title'] : '').'</p>';
 echo '<p><input type="text" style="width:98%" value="'.$meta_box_value.'" name="'.$meta_box['name'].'"></p>';
 }
 echo '<input type="hidden" name="post_newmetaboxes_noncename" id="post_newmetaboxes_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}

function hui_postmeta_thumblink_create() {
 global $theme_name;
 if ( function_exists('add_meta_box') ) {
 add_meta_box( 'postmeta_thumblink_boxes', __('外链特色图片', 'haoui'), 'hui_postmeta_thumblink', 'post', 'normal', 'high' );
 }
}
add_action( 'admin_menu', 'hui_postmeta_thumblink_create' );

function hui_postmeta_thumblink_save( $post_id ) {
 global $postmeta_thumblink;

 if ( !wp_verify_nonce( isset($_POST['post_newmetaboxes_noncename'])?$_POST['post_newmetaboxes_noncename']:'', plugin_basename(__FILE__) ))
 return;

 if ( !current_user_can( 'edit_posts', $post_id ))
 return;

 foreach($postmeta_thumblink as $meta_box) {
 $data = $_POST[$meta_box['name']];
 if(get_post_meta($post_id, $meta_box['name']) == "")
 add_post_meta($post_id, $meta_box['name'], $data, true);
 elseif($data != get_post_meta($post_id, $meta_box['name'], true))
 update_post_meta($post_id, $meta_box['name'], $data);
 elseif($data == "")
 delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
 }
}
add_action( 'save_post', 'hui_postmeta_thumblink_save' );

if ( ! function_exists( 'deel_thumbnail' ) ) {
 function deel_thumbnail() {
 global $post;
 $r_src = '';
 if ( has_post_thumbnail() ) { /* 输出设定的特色图 */
 $domsxe = simplexml_load_string(get_the_post_thumbnail());
 $thumbnailsrc = $domsxe->attributes()->src;
 $r_src = $thumbnailsrc;
 } else {
 $content = $post->post_content;
 $values = get_post_custom_values("thumblink"); /* 获取自定义栏目 thumblink */
 preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
 $n = count($strResult[1]);
 if ($values) { /* 如果有自定义栏目 thumblink,输出自定义的图片 */
 $r_src = $values[0];
 }
 else if ($n > 0) { /* 如果文章有图片,输出文章中的第一张图片 */
 $r_src = $strResult[1][0];
 } else { /* 输出默认的图片 */
 $r_src = 'http://Your image Website/xxx.png';
 }
 }
 echo '<img width="220" height="150" src="'.$r_src.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
 }
}

你可以根据自己的需要进行修改,例如:特色图的大小、默认输出的图片,如果你是高手可以修改其他地方,如果不是那么你只能修改例如出的两处。并且覆盖你所使用的主题显示特色图处的代码(不在 functions.php 中):

<a href="<?php the_permalink(); ?>"><?php deel_thumbnail(); ?></a>

把这些代码覆盖完成并保存后,在你撰写新文章时,你会在下方看到添加外链特色图地址的模块,将外链特色图的地址填写进去,例如:

http://your_thumblink/your_thumb.png

此模块你可以默认让它在下方,也可以拖动到侧面,例如跟本来就有的特色图片模块放在一起。

好了,WordPress 文章外链特色图片 就分享到这里,非常感谢你的来访。如果你喜欢本站,请不要忘记收藏本站,以便下次继续访问;也可以 关注站长微博 随时获取最新动态。你的支持就是我最大的动力!

转载请注明:爱维科斯 » WordPress 文章外链特色图片

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者