首页 > 解决方案 > 如何将 Achnor 添加到分类链接

问题描述

我正在尝试在我的代码中放置一个锚点,以便链接指向#here

举个例子

mysite/property/?jsf=jet-engine&tax=property_type:9 #这里

add_filter('term_link', function ($termlink, $term, $taxonomy) {
   
    // taxonomy type
    if ('property_type' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_type:' . $term->term_id;  
    }
    
    // taxonomy city
    if ('property_city' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_city:' . $term->term_id;
    }
    
    return $termlink;
}, 10, 3);

在我看来,它必须是.$anchor. '#here';

但这似乎不是正确的方法..(PHP致命错误)

add_filter('term_link', function ($termlink, $term, $taxonomy) {
   
    // taxonomy type
    if ('property_type' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_type:' . $term->term_id; . $anchor . '#here';
    }
    
    // taxonomy city
    if ('property_city' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_city:' . $term->term_id; . $anchor . '#here';
    }
    
    return $termlink;
}, 10, 3);

标签: phpwordpresstaxonomyadd-filter

解决方案


您只需要在之后删除分号,$term->term_id 最终代码将如下所示

add_filter('term_link', function ($termlink, $term, $taxonomy) {
    if ('property_type' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_type:' . $term->term_id . $anchor . '#here';
    }
    if ('property_city' == $taxonomy) {
        $termlink = trailingslashit(get_home_url()) . 'property/?jsf=jet-engine&tax=property_city:' . $term->term_id . $anchor . '#here';
    }
    return $termlink;
}, 10, 3);

推荐阅读