简体   繁体   中英

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.

So, the actual structure might look like this

  • WINDY HILLS

    • 1 Sunnydale Road
    • 2 Sunnydale Road
    • 10 Sunnydale Road
  • SUDDEN VALLEY

    • 1 Sudden Valley
    • 2 Sudden Valley

The slug I would like for these pages would be:

www.website.com/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. I always end up with something like 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.

$taxonomy (string) (Optional) Taxonomy. Default value: ''

Source # File: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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