【发布时间】:2020-05-06 04:16:52
【问题描述】:
我在 Wordpress 和 WooCommerce 上使用 Divi 主题,并且我使用插件创建了一个名为 Brand 的自定义分类法。 现在我需要在产品页面上显示品牌字段。我不知道 php,但如果我知道将代码放在哪里,我可以将代码添加到主题文件中。 我已经看到了很多类似场景的代码,但他们中的大多数都在谈论注册分类法,我相信我已经完成了(使用 CPTUI),所以我从这个页面上的 cmets 获取了代码:@ 987654321@
我将此代码块添加到functions.php:
/**
* Get taxonomies terms links.
*
* @see get_object_taxonomies()
*/
function wpdocs_custom_taxonomies_terms_links() {
// Get post by post ID.
if ( ! $post = get_post() ) {
return '';
}
// Get post type by post.
$post_type = $post->post_type;
// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li><a href="%1$s">%2$s</a></li>',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "\n</ul>\n";
}
}
return implode( '', $out );
}
?>
然后在 Divi 代码块中输入:
<?php echo wpdocs_custom_taxonomies_terms_links(); ?>
但是什么也没发生。控制台没有错误,什么都没有。已为此产品填充分类。请帮忙!
【问题讨论】:
标签: php wordpress woocommerce custom-taxonomy