您好!欢迎来到模板下载吧!本站资源24小时自动发货,请放心选购,一次付费,终身下载,售后请提交工单!

为WordPress分类目录添加个性化图像功能扩展(二)

半岛未凉 2017-04-09 快速入门 3015 已收录 本文共3538个字,预计阅读需要9分钟。
  • 文章介绍
  • 快速入门
  • 增值服务

在做wordpress主题开发的时候,突然被客户要求做一个页面,来展示所有的分类,且分类还是要图片形式的来展示,话说做音乐类站,分类确实需要做个类似封面的分类图像才行,可是特么的wordpress分类目录设置里值有标题、别名和描述,在描述里又不能加图片代码。。。总么办?其实我在《WordPress分类目录添加个性化图像功能扩展(一)》 里面有讲到。。。而这篇文章就涵盖了,上面说了做一个页面,来展示所有的分类,且分类还是要图片形式的来展示 好吧,我给个图你就能够理解了!

20120731

下面就直接上教程吧,首先,打开你wordpress主题文件夹下的functions.php文件夹,然后将下面的代码放入:

  1. <?php
  2. global $texonomy_slug;
  3. $texonomy_slug='category'; // texonomy slug
  4. add_action($texonomy_slug.'_add_form_fields','categoryimage');
  5. function categoryimage($taxonomy){ ?>
  6.     <div>
  7.     <label for="tag-image">分类图像</label>
  8.     <input type="text" name="tag-image" id="tag-image" value="" /><br /><span>请在此输入图像URL地址。</span>
  9. </div>
  10. <?php script_css(); }
  11. add_action($texonomy_slug.'_edit_form_fields','categoryimageedit');
  12. function categoryimageedit($taxonomy){ ?>
  13. <tr>
  14.     <th scope="row" valign="top"><label for="tag-image">图像</label></th>
  15.     <td><input type="text" name="tag-image" id="tag-image" value="<?php echo get_option('_category_image'.$taxonomy->term_id); ?>" /><br /><span>请在此输入图像URL地址。</span></td>
  16. </tr>
  17. <?php script_css(); }
  18. function script_css(){ ?>
  19. <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/category-image_thickbox.js"></script>
  20. <link rel='stylesheet' id='thickbox-css'  href='<?php echo includes_url(); ?>js/thickbox/thickbox.css' type='text/css' media='all' />
  21. <script type="text/javascript">
  22.     jQuery(document).ready(function() {
  23.     var fileInput = '';
  24.     jQuery('#tag-image').live('click',
  25.     function() {
  26.         fileInput = jQuery('#tag-image');
  27.         tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
  28.         return false;
  29.     });
  30.         window.original_send_to_editor = window.send_to_editor;
  31.     window.send_to_editor = function(html) {
  32.         if (fileInput) {
  33.             fileurl = jQuery('img', html).attr('src');
  34.             if (!fileurl) {
  35.                 fileurl = jQuery(html).attr('src');
  36.             }
  37.             jQuery(fileInput).val(fileurl);
  38.             tb_remove();
  39.         } else {
  40.             window.original_send_to_editor(html);
  41.         }
  42.     };
  43.     });
  44. </script>
  45. <?php }
  46. //edit_$taxonomy
  47. add_action('edit_term','categoryimagesave');
  48. add_action('create_term','categoryimagesave');
  49. function categoryimagesave($term_id){
  50.     if(isset($_POST['tag-image'])){
  51.         if(isset($_POST['tag-image']))
  52.             update_option('_category_image'.$term_id,$_POST['tag-image'] );
  53.     }
  54. }
  55. function print_image_function(){
  56.     $texonomy_slug='category';
  57.     $_terms = wp_get_post_terms(get_the_ID(),$texonomy_slug);
  58.     $_termsidlist=array();
  59.     $result = '';
  60.     foreach($_terms as $val){
  61.         $result .= '<div style="float:left; margin-right:2px;"><a href="'.get_term_link($val).'"><img height="22px" title="'.$val->name.'" alt="'.$val->name.'" src="'.get_option('_category_image'.$val->term_id).'" /></a></div>';
  62.     }
  63.     return $result;
  64. }
  65. add_shortcode('print-image','print_image_function');
  66. ?>

新手需要注意的是,将以上代码加入到functions.php文件的时候,看是否需要去掉头尾的

  1. <?php
  2. ?>

很多新手都是因为未去掉头部,造成了网站打不开的情况,至于老手,求别打我,我是大叔我罗嗦!
代码加入后,就会出现下图的效果
1231241242131

最后,运用到wordpress主题里,将下面的循环加入你想要现实的位置

  1. <?php
  2. $categories=get_categories($args);
  3. foreach($categories as $category) {
  4.     if ( get_option('_category_image'.$category->term_id) ){
  5.             echo '<div><a href="'.get_term_link($category).'"><img title="'.$category->name.'" alt="'.$category->name.'" src="'.get_option('_category_image'.$category->term_id).'" /></a></div>';
  6.     }
  7.     echo '<div style=" text-align:center;"><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '><b>' . $category->name.'</b></a></div>';
  8. }?>

最终的大体效果就如文章第一张图所示!

温馨提示:本文最后更新于2019年3月19日,已超过 2 年没有更新,如果文章内容或图片资源失效,请留言反馈,模板下载吧会及时处理,谢谢!

上一篇:

下一篇:

为WordPress分类目录添加个性化图像功能扩展(二):等您坐沙发呢!
大牛,别默默的看了,快来点评一下吧!:)。

您必须登录后才能发表评论哦!:)

站内登录 QQ登录 微博登录
wordpress自适应高级图片shejigh主题

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系作者

模板下载吧,累计帮助1000+用户成功建站,为草根创业提供助力!

立刻开启你的建站之旅
现在加入模板下载吧,注册一个账号
';
  • 模板下载吧拥有海量网站模板及源码,站长亲测干净无后门。

  • 注册即能下载免费模板栏目资源,帮您更快的完成网站建设。

  • 每日更新模板资源,每日精品推荐,及时获取最新模板资源流行去向。

  • 完美的售后服务,帮助草根站长、企业等成功建站。

  • 将您最爱的资源收藏,建立自己的资源库,并与朋友分享。