我很难在wordpress中顯示分類法的父母。 (CPT=property and tax = property_city)
我做了一些研究以弄清楚该怎麼做。
但是我不太清楚如何顯示它们。
我会给你一个例子,你会很快理解的。
what I have (not actual shortcode or any code! Just dynamic tag term showing taxonomy without parent)
what I want to do
理想的情况是以簡碼顯示它们,所以我將它们添加到需要的地方。
我測試了這種方法,但顯然不起作用。
我想我還是听不懂。.
function taxonomy_hierarchy() {
global $post;
$taxonomy = 'property_city'; //Put your custom taxonomy term here
$terms = wp_get_post_terms( $post->ID, $taxonomy );
foreach ( $terms as $term )
{
if ($term->parent == 0) // this gets the parent of the current post taxonomy
{$myparent = $term;}
}
echo ''.$myparent->name.'';
// Right, the parent is set, now let's get the children
foreach ( $terms as $term ) {
if ($term->parent != 0) // this ignores the parent of the current post taxonomy
{
$child_term = $term; // this gets the children of the current post taxonomy
echo ''.$child_term->name.'';
}
}
}
add_shortcode( 'child-parent', function () {
$content = "echo ''.$child_term->name.'' echo ''.$myparent->name.''";
return $content;
} );
代碼在這裏找到:鏈接< / a>
UPDATE 04/01/2021
數百万感谢Stef 我在迴圈中进行了測試,並且發佈後效果很好。
function taxonomy_hierarchy() {
global $post;
$post_id = $post->ID;
$return = '';
$terms = wp_get_post_terms( $post->ID, 'property_city' ); //Put your custom taxonomy term here
foreach ( $terms as $term ) {
// this gets the parent of the current post taxonomy
//attempt to make terms clickable
$term_link = get_term_link( $term );
if ($term->parent != 0) {
$return .= $term->name. ', ' . get_term( $term->parent, 'property_city' )->name;
} else {
$return .= $term->name;
}
}
return $return;
}
add_shortcode( 'city-area', 'taxonomy_hierarchy' );
最终了解了如何进行簡碼及其背後的邏輯。 当我尝試了解php背後的邏輯時,我忘了提起術語 clickable get_term_link我應该可以 enable and disable 他们(父母或child或两者)帶有" //"(可選) How to do that ?
我已经有了這段代碼,可以將它们重定向到我的自定義請求(例如,在elementor生成器內部),因為無法單击shortcode,因此對它们不起作用。
add_filter ('term_link', function ($ termlink, $ term, $ taxonomy) {
// taxonomy city
if ('property_city' == $ taxonomy) {
$ termlink = trailingslashit (get_home_url ()). 'property /?_city ='. $ term-> slug;
}
return $ termlink;
}, 10, 3);
经過一番思考,如果有一天我需要這樣的东西...
如何使它 flexible 並顯示可用條款 hierarchically ? 我认為這 wordPress:获取分類層次結構,包括子級 与我的問题有關吗?
但是我不能把难题拼凑起来
感谢您的耐心
最新回復
- 5月前1 #
您的功能
taxonomy_hierarchy()
實際上没有鏈接到簡碼.您的功能taxonomy_hierarchy()
也不是正確的簡碼功能。 您可以在此處找到如何建立適当的簡碼。簡碼功能還必须返迴,不要使用echo。
這就是您的功能
taxonomy_hierarchy
應该kinda外观.我已根据您的需要更改了代碼:然後將其添加以从
taxonomy_hierarchy()
函式建立簡碼使用簡碼前端時,可以使用
[child-parent]
.可以使用echo do_shortcode( '[child-parent]' );
的後端