【问题标题】:Custom Taxonomy Terms in Custom Post Type Slug自定义帖子类型段中的自定义分类术语
【发布时间】:2018-01-08 11:50:07
【问题描述】:

对于一个住房网站,我们有以下安排,其中有地块(又名房屋)被分组为开发项目。

所以,实际的结构可能是这样的

  • 风山

    • 桑尼代尔路 1 号
    • 桑尼代尔路 2 号
    • 桑尼代尔路10号
  • 突然谷

    • 1 突如其来的山谷
    • 2 突如其来的山谷

我希望这些页面的 slug 是:

www.website.com/sudden-valley

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

我已经尝试了多种方法。我认为具有“发展”分类的自定义帖子类型“情节”效果最好。但是我无法弄清楚如何将分类术语放入 url。我总是以类似 www.website.com/developments/sudden-valley 之类的东西结束

除了蛞蝓,我对自定义帖子类型/分类法非常精通,很高兴尝试任何建议 - 认为这是一张白纸。非常感谢任何帮助。

【问题讨论】:

    标签: wordpress custom-taxonomy taxonomy-terms


    【解决方案1】:

    为分类术语生成固定链接

    参数

    $术语 (object|int|string) (必需)将检索其链接的术语对象、ID 或 slug。

    $分类 (字符串)(可选)分类。 默认值:''

    来源# 文件: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 );
    }
    

    还有像 Custom Post Type Permalinks 这样的插件可以为您做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      相关资源
      最近更新 更多