简体   繁体   English

自定义帖子类型条中的自定义分类术语

[英]Custom Taxonomy Terms in Custom Post Type Slug

We have the following arrangement for a housing website which has plots (aka houses) which are grouped into developments. 对于住房网站,我们将按照以下安排进行安排:将地块(aka房屋)归为开发项目。

So, the actual structure might look like this 因此,实际结构可能如下所示

  • WINDY HILLS 风丘

    • 1 Sunnydale Road 1 Sunnydale路
    • 2 Sunnydale Road 森尼戴尔路2号
    • 10 Sunnydale Road 森尼戴尔路10号
  • SUDDEN VALLEY 突谷

    • 1 Sudden Valley 1突谷
    • 2 Sudden Valley 2突谷

The slug I would like for these pages would be: 我想要这些页面的子弹是:

www.website.com/sudden-valley www.website.com/sudden-valley

www.website.com/sudden-valley/1-sudden-valley www.website.com/sudden-valley/1-sudden-valley

I've tried multiple approaches to this. 我已经尝试了多种方法。 I think a custom post type of 'plots' with a taxonomy of 'developments' would work best. 我认为以“发展”为分类标准的“职位”自定义职位类型最为有效。 However I cannot figure out how to get the taxonomy term into the url. 但是,我无法弄清楚如何将分类术语输入URL。 I always end up with something like www.website.com/developments/sudden-valley 我总是以类似www.website.com/developments/sudden-valley的结尾

Aside from slugs, I'm pretty savvy with custom post types / taxonomies, so happy to try anything suggested - consider this a blank slate. 除了之外,我对自定义帖子类型/分类法也非常了解,因此很乐意尝试任何建议的方法-将其视为空白。 Any help much appreciated. 任何帮助,不胜感激。

Generate a permalink for a taxonomy term 生成分类术语的固定链接

Parameters 参数

$term (object|int|string) (Required) The term object, ID, or slug whose link will be retrieved. $ term(object | int | string)(必需)将检索其链接的术语object,ID或slug。

$taxonomy (string) (Optional) Taxonomy. $ taxonomy(字符串)(可选)。 Default value: '' 默认值: ''

Source # File: wp-includes/taxonomy.php 源代码文件:wp-includes / taxonomy.php

function get_term_link( $term, $taxonomy = '' ) {
    global $wp_rewrite;

    if ( !is_object($term) ) {
        if ( is_int( $term ) ) {
            $term = get_term( $term, $taxonomy );
        } else {
            $term = get_term_by( 'slug', $term, $taxonomy );
        }
    }

    if ( !is_object($term) )
        $term = new WP_Error('invalid_term', __('Empty Term'));

    if ( is_wp_error( $term ) )
        return $term;

    $taxonomy = $term->taxonomy;

    $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);

    $slug = $term->slug;
    $t = get_taxonomy($taxonomy);

    if ( empty($termlink) ) {
        if ( 'category' == $taxonomy )
            $termlink = '?cat=' . $term->term_id;
        elseif ( $t->query_var )
            $termlink = "?$t->query_var=$slug";
        else
            $termlink = "?taxonomy=$taxonomy&term=$slug";
        $termlink = home_url($termlink);
    } else {
        if ( $t->rewrite['hierarchical'] ) {
            $hierarchical_slugs = array();
            $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
            foreach ( (array)$ancestors as $ancestor ) {
                $ancestor_term = get_term($ancestor, $taxonomy);
                $hierarchical_slugs[] = $ancestor_term->slug;
            }
            $hierarchical_slugs = array_reverse($hierarchical_slugs);
            $hierarchical_slugs[] = $slug;
            $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
        } else {
            $termlink = str_replace("%$taxonomy%", $slug, $termlink);
        }
        $termlink = home_url( user_trailingslashit($termlink, 'category') );
    }
    // Back Compat filters.
    if ( 'post_tag' == $taxonomy ) {

        /**
         * Filters the tag link.
         *
         * @since 2.3.0
         * @deprecated 2.5.0 Use 'term_link' instead.
         *
         * @param string $termlink Tag link URL.
         * @param int    $term_id  Term ID.
         */
        $termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
    } elseif ( 'category' == $taxonomy ) {

        /**
         * Filters the category link.
         *
         * @since 1.5.0
         * @deprecated 2.5.0 Use 'term_link' instead.
         *
         * @param string $termlink Category link URL.
         * @param int    $term_id  Term ID.
         */
        $termlink = apply_filters( 'category_link', $termlink, $term->term_id );
    }

    /**
     * Filters the term link.
     *
     * @since 2.5.0
     *
     * @param string $termlink Term link URL.
     * @param object $term     Term object.
     * @param string $taxonomy Taxonomy slug.
     */
    return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}

There are also plugins like Custom Post Type Permalinks that can do this for you. 还有一些插件,例如“ 自定义帖子类型”永久链接可以为您完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM