Custom taxonomy on permalink

时间:2013-02-20 作者:elenakoroleva

我有一个很长时间的问题,期待你的帮助。我有一个自定义的“城市”分类法(它不像标记那样具有层次结构)。我只希望我的URL如下所示:

/%city%/%category%/%postname%/
这就是我想要完成的。

  • http://example.com/city/moscow - 显示分类城市莫斯科的所有帖子
  • http://example.com/category/shopping - 显示此类别中的所有帖子和所有城市
  • http://example.com/category/shopping/supermarket - 显示此类别中的所有帖子和所有城市
  • http://example.com/moscow/shopping/ -在莫斯科显示此类别的所有帖子
  • http://example.com/moscow/shopping/supermarket/ - 在莫斯科显示此子类别中的所有帖子
  • http://example.com/moscow/shopping/supermarket/postname - 显示帖子
I’ve made the following:

首先,我创建了一个自定义分类法-城市

function add_custom_taxonomies_city() {
    register_taxonomy(\'city\', \'post\', array(
        \'hierarchical\' => false,
        \'labels\' => array(
            \'name\' => _x( \'City\', \'taxonomy general name\' ),
            \'search_items\' =>  __( \'Search cities\' ),
            \'all_items\' => __( \'All cities\' ),
            \'parent_item\' => __( \'Parent Categories\' ),
            \'parent_item_colon\' => __( \'Parent Categories:\' ),
            \'edit_item\' => __( \'Edit city\' ), 
            \'update_item\' => __( \'Update city\' ),
            \'add_new_item\' => __( \'Add city\' ),
            \'new_item_name\' => __( \'New city\' ),
            \'menu_name\' => __( \'cities\' ),
            );    

        ),
    \'rewrite\' => array(
        \'slug\' => \'city\',
        \'with_front\' => false,
        \'hierarchical\' => false
        ),
    ));
}
add_action( \'init\', \'add_custom_taxonomies_city\', 0 ); 

add_filter(\'post_link\', \'town_permalink\', 10, 3);
add_filter(\'post_type_link\', \'town_permalink\', 10, 3);

function town_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, \'%city%\') === FALSE) return $permalink;

// Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;

// Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, \'city\');  
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = \'not-city\';

    return str_replace(\'%city%\', $taxonomy_slug, $permalink);
}
The result:

  • http://example.com/city/moscow - 它工作正常http://example.com/category/shopping - 它工作正常http://example.com/category/shopping/supermarket/ - 它工作正常http://example.com/moscow/shopping/ - 它工作正常http://example.com/moscow/shopping/supermarket/ - 它没有
  • http://example.com/moscow/shopping/supermarket/postname – 它没有http: //mysite.ru/ moscow/shopping/postname

    但我需要包含子类别的链接。

    我尝试用另一个选项来解决该任务:我在函数中添加了以下代码。php

  • function addRoutes() {
        add_rewrite_tag(\'%city%\', \'(.+?)\');
        add_rewrite_rule(\'(.+?)/(.+?)/([^/]+)(/[0-9]+)?/?$\', \'index.php?city=$matches[1]&category_name=$matches[2]&name=$matches[3]&page=$matches[4]\', \'top\');
      add_rewrite_rule(\'(.?.+?)(/[0-9]+)?/?$\', \'index.php?pagename=$matches[1]&page=$matches[2]\', \'top\');
        flush_rewrite_rules();
    }
    add_action(\'init\', \'addRoutes\');
    
    As a result

    http://example.com/city/moscow - 找不到页面404错误。http://example.com/category/shopping - 找不到页面404错误。。http://example.com/category/shopping/supermarket/ - 找不到页面404错误。。http://example.com/moscow/shopping/supermarket/postname –400错误

    系统似乎不明白%city%怎么了?期待你的帮助有人能帮我吗?非常感谢!

    我决定将URL的结构更改为:

    • example.com/city/moscow/category/shopping/ -在莫斯科显示此类别的所有帖子
    • example.com/city/moscow/category/shopping/supermarket/ - 在莫斯科显示此子类别中的所有帖子
    • example.com/city/moscow/category/shopping/supermarket/postname - 显示帖子
    代码:

    //The rewrite rules
    function eg_add_query_vars( $query_vars ) {
        $new_vars = array( \'town\' );
    
        return array_merge( $new_vars, $query_vars );
    }
    add_filter( \'query_vars\', \'eg_add_query_vars\' );
    
    function eg_add_rewrite_rules() {
        global $wp_rewrite;
    
        $new_rules = array(
            \'city/(.+?)/category/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2),
    \'city/(.+?)/category/(.+?)/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2) . \'&category_name=\'. $wp_rewrite->preg_index(3),
    
    //\'city/(.+?)/category/(.+?)/(.+?)/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2) . \'&category_name=\'. $wp_rewrite->preg_index(3). \'&page=\'. $wp_rewrite->preg_index(4),// This is me effort to write the rule for posts (doesn\'t work)   
            );
        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_action( \'generate_rewrite_rules\', \'eg_add_rewrite_rules\' );
    
    
    //creating the permalink:
    function city_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, \'%city%\') === FALSE) return $permalink;
    
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, \'town\');  
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = \'not-city\';
    
        return str_replace(\'%town%\', $taxonomy_slug, $permalink);}
    
        function wp_town_query( $query ) {
        if( isset( $query->query_vars[\'city\'] ) ):
            if( $city = get_term_by( \'id\', $query->query_vars[\'city\'], \'city\' ) )
                $query->query_vars[\'town\'] = $city->slug;
        endif;
    }
    add_action( \'parse_query\', \'wp_city_query\' );
    
    Permalink管理页面中的我的设置:城市/%city%/类别/%category%/%postname%/

    因此,我有:-example.com/city/moscow/category/shopping/ -工程-example.com/city/moscow/category/shopping/supermarket/ - 工作,但我仍然不明白如何为帖子编写规则:-example.com/city/moscow/category/shopping/supermarket/postname - 不起作用。分页也不起作用。

    我很乐意得到任何建议!

    这是工作:

    
    \'city/(.+?)/category/(.+?)/(.+?)/([^/]+)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2). \'&category_name=\' . $wp_rewrite->preg_index(3). \'&name=\' . $wp_rewrite->preg_index(4), 
    \'city/(.+?)/category/(.+?)/page/?([0-9]{1,})/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2). \'&paged=\' . $wp_rewrite->preg_index(3),
    \'city/(.+?)/category/(.+?)/?$\' => \'index.php?city=\' . $wp_rewrite->preg_index(1) . \'&category_name=\' . $wp_rewrite->preg_index(2),  
    

1 个回复
SO网友:user1576978

好的,我会得到一些负面的观点,但这应该可以解决你的问题,请不要生我的气。在任何人对我投反对票之前,我必须说我与这个插件的开发人员没有任何关系:

对于自定义分类法和自定义帖子类型,我经常使用名为“Types“。你可以在wordpress菜单上的install plugins中找到它,并且不需要任何编码就可以完成这项任务。

结束

相关推荐

Custom Taxonomy Query

我为我创建的自定义帖子类型设置了两个自定义分类法。post类型称为beer,两种分类法是country和brewers。我想这样列出他们。 Country -->Brewers ----->Beers 我可以使用此代码提取国家。 $terms_country = get_terms(\'country\'); foreach ($terms_country as $term_country) { echo \"<h3 c