最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress给分类目录添加缩略图的方法
时间:2022-06-25 18:37:37 编辑:袖梨 来源:一聚教程网
https://img.111cn.net方法1、Categories Images插件。https://img.111cn.nethttps://img.111cn.net
https://img.111cn.netCategories Images是一款扩展wordpress分类功能、添加分类图片的插件,该插件支持默认wordpress的文章类型,也支持wordpressP自定义文章类型以及多站点。这个插件使用比较简单,可以轻松上手,这里就不多做介绍 ,网上这方面的 次料很多。https://img.111cn.net
https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net
https://img.111cn.nethttps://img.111cn.net方法2、代码实现。https://img.111cn.nethttps://img.111cn.net
https://img.111cn.net个人还是比较喜欢直接代码实现,这样做,既可以让网站不会有太多的插件而影响速度,也可以让wordpress主题集成更多的功能,这也是wordpress控们比较喜欢的方式,这就不多说了。其实,代码也是从插件中提练出来的,只是做了一些相应的改动。https://img.111cn.net
第一步:在主题functions.php文件中添加如下代码:https://img.111cn.net
/**https://img.111cn.net
* Plugin Name: 分类图像https://img.111cn.net
*/https://img.111cn.net
define('Z_IMAGE_PLACEHOLDER', T_PATH."/img/random/5.jpg"); //默认https://img.111cn.net缩略图https://img.111cn.nethttps://img.111cn.net
add_action('admin_init', 'z_init');https://img.111cn.net
function z_init() {https://img.111cn.net
$z_taxonomies = get_taxonomies();https://img.111cn.net
if (is_array($z_taxonomies)) {https://img.111cn.net
$zci_options = get_option('zci_options');https://img.111cn.net
if (empty($zci_options['excluded_taxonomies']))https://img.111cn.net
$zci_options['excluded_taxonomies'] = array();https://img.111cn.net
https://img.111cn.netforeachhttps://img.111cn.net ($z_taxonomies as $z_taxonomy) {https://img.111cn.net
if (in_array($z_taxonomy, $zci_options['excluded_taxonomies']))https://img.111cn.net
continue;https://img.111cn.net
add_action($z_taxonomy.'_add_form_fields', 'z_add_texonomy_field');https://img.111cn.net
add_action($z_taxonomy.'_edit_form_fields', 'z_edit_texonomy_field');https://img.111cn.net
add_filter( 'manage_edit-' . $z_taxonomy . '_columns', 'z_taxonomy_columns' );https://img.111cn.net
add_filter( 'manage_' . $z_taxonomy . '_custom_column', 'z_taxonomy_column', 10, 3 );https://img.111cn.net
}https://img.111cn.net
}https://img.111cn.net
}https://img.111cn.net
// add image field in add formhttps://img.111cn.net
function z_add_texonomy_field() {https://img.111cn.net
if (get_bloginfo('version') >= 3.5)https://img.111cn.net
wp_enqueue_media();https://img.111cn.net
else {https://img.111cn.net
wp_enqueue_style('thickbox');https://img.111cn.net
wp_enqueue_script('thickbox');https://img.111cn.net
}https://img.111cn.net
}https://img.111cn.net
// 在编辑表单中添加图像字段https://img.111cn.net
function z_edit_texonomy_field($taxonomy) {https://img.111cn.net
if (get_bloginfo('version') >= 3.5)https://img.111cn.net
wp_enqueue_media();https://img.111cn.net
else {https://img.111cn.net
wp_enqueue_style('thickbox');https://img.111cn.net
wp_enqueue_script('thickbox');https://img.111cn.net
}https://img.111cn.net
if (z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE ) == Z_IMAGE_PLACEHOLDER)https://img.111cn.net
$image_text = "";https://img.111cn.net
elsehttps://img.111cn.net
$image_text = z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE );https://img.111cn.net
echo 'https://img.111cn.net
https://img.111cn.net
https://img.111cn.net
https://img.111cn.nethttps://img.111cn.net
https://img.111cn.nethttps://img.111cn.net
https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net
https://img.111cn.net
https://img.111cn.net
}https://img.111cn.net
// 图片上传函数https://img.111cn.net
function z_script() {https://img.111cn.net
return 'https://img.111cn.net';https://img.111cn.net
}https://img.111cn.net
// 保存分类图像,同时编辑或保存期限https://img.111cn.net
add_action('edit_term','z_save_taxonomy_image');https://img.111cn.net
add_action('create_term','z_save_taxonomy_image');https://img.111cn.net
function z_save_taxonomy_image($term_id) {https://img.111cn.net
if(isset($_POST['taxonomy_image']))https://img.111cn.net
update_option('z_taxonomy_image'.$term_id, $_POST['taxonomy_image']);https://img.111cn.net
}https://img.111cn.net
// 通过图片网址获取附件https://img.111cn.net
function z_get_attachment_id_by_url($image_src) {https://img.111cn.net
global $wpdb;https://img.111cn.net
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid = '$image_src'";https://img.111cn.net
$id = $wpdb->get_var($query);https://img.111cn.net
return (!empty($id)) ? $id : NULL;https://img.111cn.net
}https://img.111cn.net
// 对于给定的term_id得到分类图像的URL(默认占位符图像)https://img.111cn.net
function z_taxonomy_image_url($term_id = NULL, $size = NULL, $return_placeholder = FALSE) {https://img.111cn.net
if (!$term_id) {https://img.111cn.net
if (is_category())https://img.111cn.net
$term_id = get_query_var('cat');https://img.111cn.net
elseif (is_tax()) {https://img.111cn.net
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));https://img.111cn.net
$term_id = $current_term->term_id;https://img.111cn.net
}https://img.111cn.net
}https://img.111cn.net
$taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);https://img.111cn.net
if(!empty($taxonomy_image_url)) {https://img.111cn.net
$attachment_id = z_get_attachment_id_by_url($taxonomy_image_url);https://img.111cn.net
if(!empty($attachment_id)) {https://img.111cn.net
if (empty($size))https://img.111cn.net
$size = 'full';https://img.111cn.net
$taxonomy_image_url = wp_get_attachment_image_src($attachment_id, $size);https://img.111cn.net
$taxonomy_image_url = $taxonomy_image_url[0];https://img.111cn.net
}https://img.111cn.net
}https://img.111cn.net
if ($return_placeholder)https://img.111cn.net
return ($taxonomy_image_url != '') ? $taxonomy_image_url : Z_IMAGE_PLACEHOLDER;https://img.111cn.net
elsehttps://img.111cn.net
return $taxonomy_image_url;https://img.111cn.net
}https://img.111cn.net
function z_quick_edit_custom_box($column_name, $screen, $name) {https://img.111cn.net
if ($column_name == 'thumb')https://img.111cn.net
echo 'https://img.111cn.net';https://img.111cn.net
}https://img.111cn.net
// 缩略图列添加到类别管理https://img.111cn.net
function z_taxonomy_columns( $columns ) {https://img.111cn.net
$new_columns = array();https://img.111cn.net
$new_columns['cb'] = $columns['cb'];https://img.111cn.net
$new_columns['thumb'] = '图像';https://img.111cn.net
unset( $columns['cb'] );https://img.111cn.net
return array_merge( $new_columns, $columns );https://img.111cn.net
}https://img.111cn.net
// 缩略图列值添加到类别管理。https://img.111cn.net
function z_taxonomy_column( $columns, $column, $id ) {https://img.111cn.net
if ( $column == 'thumb' )https://img.111cn.net
$columns = 'https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net';https://img.111cn.net
return $columns;https://img.111cn.net
}https://img.111cn.net
// “更改”插入“使用该图像”https://img.111cn.net
function z_change_insert_button_text($safe_text, $text) {https://img.111cn.net
return str_replace("Insert into Post", "Use this image", $text);https://img.111cn.net
}https://img.111cn.net
// 在类别列表中的图像https://img.111cn.net
if ( strpos( $_SERVER['SCRIPT_NAME'], 'edit-tags.php' ) > 0 ) {https://img.111cn.net
add_action( 'admin_head', 'z_add_style' );https://img.111cn.net
add_action('quick_edit_custom_box', 'z_quick_edit_custom_box', 10, 3);https://img.111cn.net
add_filter("attribute_escape", "z_change_insert_button_text", 10, 2);https://img.111cn.net
}https://img.111cn.net
// 注册插件设置https://img.111cn.net
function z_register_settings() {https://img.111cn.net
register_setting('zci_options', 'zci_options', 'z_options_validate');https://img.111cn.net
add_settings_section('zci_settings', '', 'z_section_text', 'zci-options');https://img.111cn.net
add_settings_field('z_excluded_taxonomies', '排除的分类','z_excluded_taxonomies', 'zci-options', 'zci_settings');https://img.111cn.net
}https://img.111cn.net
function z_add_style() {https://img.111cn.net
echo 'https://img.111cn.net';https://img.111cn.net
}https://img.111cn.net
如果觉得代码比较多,也可以把它放到一个单独的php文件中,然后,再在functions.php文件中引用它。https://img.111cn.net
第二步:在前台页面模板中调用这个分类缩略图。https://img.111cn.net
在当前分类目录页面调用代码:https://img.111cn.net
https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net
https://img.111cn.net 或者https://img.111cn.net
https://img.111cn.nethttps://img.111cn.net
https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net
https://img.111cn.net https://img.111cn.nethttps://img.111cn.net
在其它页面调用代码:这里需要我添加一个分类目录ID号https://img.111cn.net
https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net
https://img.111cn.net 或者https://img.111cn.net
https://img.111cn.net if(function_exists('z_taxonomy_image_url')&&(z_taxonomy_image_url(栏目ID号)!='')){https://img.111cn.net
$img_url = z_taxonomy_image_url(栏目ID号);https://img.111cn.net
}else{https://img.111cn.net
$img_url = T_PATH.'/img/banner/catt.jpg'; //默认图片https://img.111cn.net
}https://img.111cn.net
?>https://img.111cn.net
https://img.111cn.nethttps://img.111cn.nethttps://img.111cn.nethttps://img.111cn.net https://img.111cn.net
通过上面的代码,我们就实现了在wordspress分类目录中添加了缩略图功能模块,如果你也想让自己的网站主题的不同分类目录显示不同的banner图片的话,那就不防用一用上面的2种方法,从此你的网站就与众不同哦。https://img.111cn.net
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22