如何为WordPress的taxonomy分类法链接添加锚点#here
错误原因
- 语法错误:你在
$term->term_id后多余添加了英文分号,将完整的赋值语句截断,后续拼接代码变为无效独立语句,触发PHP致命错误。 - 无效变量:代码中并未定义
$anchor变量,无需额外添加该变量,直接在链接末尾拼接目标锚点#here即可。
修正后可正常运行的代码
add_filter('term_link', function ($termlink, $term, $taxonomy) { // 处理 property_type 分类链接 if ('property_type' == $taxonomy) { $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_type:' . $term->term_id . '#here'; } // 处理 property_city 分类链接 if ('property_city' == $taxonomy) { $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_city:' . $term->term_id . '#here'; } return $termlink; }, 10, 3);
如果需要后续使用动态锚点,只需提前定义锚点变量替换代码中的#here即可。
内容的提问来源于stack exchange,提问作者neosan
相关产品推荐
相关产品推荐

