![](/img/trans.png)
[英]Wordpress - Including custom Taxonomy Slug into Custom Post-Type Slug
[英]Custom Taxonomy Terms in Custom Post Type Slug
對於住房網站,我們將按照以下安排進行安排:將地塊(aka房屋)歸為開發項目。
因此,實際結構可能如下所示
風丘
- 1 Sunnydale路
- 森尼戴爾路2號
- 森尼戴爾路10號
突谷
- 1突谷
- 2突谷
我想要這些頁面的子彈是:
www.website.com/sudden-valley
www.website.com/sudden-valley/1-sudden-valley
我已經嘗試了多種方法。 我認為以“發展”為分類標准的“職位”自定義職位類型最為有效。 但是,我無法弄清楚如何將分類術語輸入URL。 我總是以類似www.website.com/developments/sudden-valley的結尾
除了之外,我對自定義帖子類型/分類法也非常了解,因此很樂意嘗試任何建議的方法-將其視為空白。 任何幫助,不勝感激。
生成分類術語的固定鏈接
$ term(object | int | string)(必需)將檢索其鏈接的術語object,ID或slug。
$ taxonomy(字符串)(可選)。 默認值: ''
源代碼文件: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 );
}
還有一些插件,例如“ 自定義帖子類型”永久鏈接可以為您完成此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.