如何在WordPress多文件中共享PHP分类法变量避免重复定义
嘿,这个需求在WordPress开发里挺常见的,我给你几种靠谱的实现方式,不用重复写那些复制粘贴的代码:
方法1:封装为自定义函数(强烈推荐)
这是最符合WordPress编码规范的做法,把获取分类数据的逻辑封装成一个可复用的函数,放在主题的functions.php里,之后在任何需要的模板文件里调用就行,还能避免全局变量污染。
步骤1:在functions.php中添加函数
function get_post_taxonomy_data($post_id = null) { // 默认使用当前文章ID,也可以手动传入指定文章ID if (!$post_id) { $post_id = get_the_ID(); } $taxonomy_data = []; // 获取「themas」分类数据 $themes_terms = wp_get_post_terms($post_id, 'themas'); if (!is_wp_error($themes_terms) && !empty($themes_terms)) { $taxonomy_data['themes'] = [ 'name' => $themes_terms[0]->name, 'slug' => $themes_terms[0]->slug, 'link' => get_term_link($themes_terms[0]->slug, 'themas') ]; } // 获取「rubrieken」分类数据 $rubrieken_terms = wp_get_post_terms($post_id, 'rubrieken'); if (!is_wp_error($rubrieken_terms) && !empty($rubrieken_terms)) { $taxonomy_data['rubrieken'] = [ 'name' => $rubrieken_terms[0]->name, 'slug' => $rubrieken_terms[0]->slug, 'link' => get_term_link($rubrieken_terms[0]->slug, 'rubrieken') ]; } return $taxonomy_data; }
步骤2:在需要的模板文件中调用
比如在single.php、page.php或者其他模板里:
// 获取分类数据 $taxonomy_data = get_post_taxonomy_data(); // 使用「themas」的数据 if (isset($taxonomy_data['themes'])) { echo '主题:' . esc_html($taxonomy_data['themes']['name']); echo '<a href="' . esc_url($taxonomy_data['themes']['link']) . '">查看更多</a>'; } // 使用「rubrieken」的数据同理 if (isset($taxonomy_data['rubrieken'])) { echo '分类:' . esc_html($taxonomy_data['rubrieken']['name']); }
注意:我加了is_wp_error和非空判断,避免文章没有分类时出现PHP报错,同时用esc_html和esc_url做安全转义,符合WordPress安全规范。
方法2:使用全局变量(不推荐,仅作参考)
如果你一定要用全局变量的方式,也可以实现,但要注意全局变量容易和其他插件/主题的变量冲突,而且在非单页模板(比如首页)调用时可能出问题。
步骤1:在single.php中声明全局变量
global $taxonomy_themes_singular_name, $taxonomy_rubrieken_singular_name, $taxonomy_themes_singular_slug, $taxonomy_rubrieken_singular_slug, $taxonomy_themes_singular_link, $taxonomy_rubrieken_singular_link; $taxonomy_themes = wp_get_post_terms(get_the_ID(), 'themas'); $taxonomy_rubrieken = wp_get_post_terms(get_the_ID(), 'rubrieken'); // 处理themas数据,加错误判断 if (!is_wp_error($taxonomy_themes) && !empty($taxonomy_themes)) { $taxonomy_themes_singular_name = $taxonomy_themes[0]->name; $taxonomy_themes_singular_slug = $taxonomy_themes[0]->slug; $taxonomy_themes_singular_link = get_term_link($taxonomy_themes_singular_slug, 'themas'); } // 处理rubrieken数据,加错误判断 if (!is_wp_error($taxonomy_rubrieken) && !empty($taxonomy_rubrieken)) { $taxonomy_rubrieken_singular_name = $taxonomy_rubrieken[0]->name; $taxonomy_rubrieken_singular_slug = $taxonomy_rubrieken[0]->slug; $taxonomy_rubrieken_singular_link = get_term_link($taxonomy_rubrieken_singular_slug, 'rubrieken'); }
步骤2:在其他文件中调用全局变量
比如在header.php或者footer.php里:
// 先声明要使用的全局变量 global $taxonomy_themes_singular_name, $taxonomy_rubrieken_singular_name; // 使用前先检查变量是否存在,避免报错 if (isset($taxonomy_themes_singular_name)) { echo $taxonomy_themes_singular_name; }
方法3:利用WordPress钩子全局初始化
如果需要在整个网站的模板中都能调用这些数据,可以用wp钩子提前初始化,同样放在functions.php里:
global $global_post_taxonomy_data; add_action('wp', 'init_global_taxonomy_data'); function init_global_taxonomy_data() { global $global_post_taxonomy_data; $post_id = get_the_ID(); // 直接复用方法1里的函数 $global_post_taxonomy_data = get_post_taxonomy_data($post_id); }
之后在任何模板文件里就可以直接调用:
global $global_post_taxonomy_data; if (isset($global_post_taxonomy_data['themes'])) { echo $global_post_taxonomy_data['themes']['name']; }
总的来说,方法1是最推荐的,代码整洁、可维护性高,还能避免很多潜在问题。
内容的提问来源于stack exchange,提问作者user10431099
相关产品推荐
相关产品推荐

