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

本文章介绍如何在WordPress后台为分类目录添加自定义图像,提供代码实现及使用说明,不受仅限分类名称或别名命名图片的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

偶米工作室不断积累WordPress使用和二次开发经验,在此为大家奉献一个独家经验,就是为WordPress分类目录添加图像,注意这个图片是在后台添加的,即在仪表盘中为分类目录指定图像目录,不同于别人分享的“自定义WordPress分类图片”,两者区别在于,前者可以为分类指定任意图片,而网友的方法只能是限定于以“分类名称”或“别名”命名的图片,限制多多。使用我们的经验,可以为分类指定任何图片,不限图片地址、不限图片格式等等,功能多多哦。

转自:http://www.oome.org/wordpress-category-image.html

<?php

global $texonomy_slug;
$texonomy_slug='category'; // texonomy slug
add_action($texonomy_slug.'_add_form_fields','categoryimage');
function categoryimage($taxonomy){ ?>
    <div>
    <label for="tag-image">分类图像</label>
    <input type="text" name="tag-image" id="tag-image" value="" /><br /><span>请在此输入图像URL地址。</span>    
</div>
<?php script_css(); }
add_action($texonomy_slug.'_edit_form_fields','categoryimageedit');
function categoryimageedit($taxonomy){ ?>
<tr>
    <th scope="row" valign="top"><label for="tag-image">图像</label></th>
    <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>
</tr>              
<?php script_css(); }
function script_css(){ ?>                
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/category-image_thickbox.js"></script>
<link rel='stylesheet' id='thickbox-css'  href='<?php echo includes_url(); ?>js/thickbox/thickbox.css' type='text/css' media='all' />
<script type="text/javascript">    
    jQuery(document).ready(function() {
    var fileInput = '';
    jQuery('#tag-image').live('click',
    function() {
        fileInput = jQuery('#tag-image');
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
    });
        window.original_send_to_editor = window.send_to_editor;
    window.send_to_editor = function(html) {
        if (fileInput) {
            fileurl = jQuery('img', html).attr('src');
            if (!fileurl) {
                fileurl = jQuery(html).attr('src');
            }
            jQuery(fileInput).val(fileurl);
            tb_remove();
        } else {
            window.original_send_to_editor(html);
        }
    };
    });
</script>
<?php }
//edit_$taxonomy
add_action('edit_term','categoryimagesave');
add_action('create_term','categoryimagesave');
function categoryimagesave($term_id){
    if(isset($_POST['tag-image'])){
        if(isset($_POST['tag-image']))
            update_option('_category_image'.$term_id,$_POST['tag-image'] );
    }
}
function print_image_function(){
    $texonomy_slug='category';
    $_terms = wp_get_post_terms(get_the_ID(),$texonomy_slug);
    $_termsidlist=array();  
    $result = '';
    foreach($_terms as $val){    
        $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>';    
    }
    return $result;
}
add_shortcode('print-image','print_image_function');
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值