请原谅,我的Wordpress/PHP技能非常初级,所以我可能没有使用正确的术语。
我在WooCommerce单一产品页面中添加了一个自定义选项卡,需要它显示与自定义分类法“book author”相关的大量术语元值(有时单个,有时多个)。这些值在所见即所得编辑器中创建,并包含HTML格式。每个“图书作者”都有一个作者传记(术语meta“wpcf作者传记”),每个WooCommerce产品都有一个或多个“图书作者”。
这是我当前使用的代码:
// Add author bio tab
add_filter( \'woocommerce_product_tabs\', \'woo_new_product_tab\' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs[\'author_tab\'] = array(
\'title\' => __( \'About the author\', \'woocommerce\' ),
\'priority\' => 50,
\'callback\' => \'woo_new_product_tab_content\'
);
return $tabs;
}
// Add the content
function woo_new_product_tab_content() {
$terms = get_the_terms(get_the_ID(), \'book-author\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$all_term_meta = get_term_meta($term->term_id, \'wpcf-author-biography\', false);
print_r($all_term_meta);
}
}
}
我花了很长时间才达到这一点,虽然相关数据最终被输出,但前端结果仍有很多需要改进的地方。结果是这样的:
<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">
Array
(
[0] => XX staff writer <strong>John Doe</strong> has covered
everything from men’s style and grooming to food, drinks and travel. John
has also interviewed some of the biggest names in film, television and
music.
)
Array
(
[0] => <div class="woocommerce-tabs wc-tabs-wrapper">
<div id="tab-test_tab" class="woocommerce-Tabs-panel woocommerce-Tabs-panel--
test_tab panel entry-content wc-tab" style="display: none;">
<strong>Jane Doe </strong>is an author and university
lecturer in underwater basket-weaving. She has recently completed a PhD.
</div>
</div>
)
</div>
虽然选项卡本身创建时没有任何问题,但正如您所见,我遇到了一些流氓
Arrays
第二个术语元值由于某种原因被卡在了它自己的隐藏标签包装中,而它应该出现在同一个标签中。
很明显,我不会以正确的方式来做这件事,否则我就不会在这里了,所以如果能在正确的方向上提供帮助或推动,我将不胜感激。
此外,如果有任何方法实现一个条件,以便在产品没有“图书作者”属性的情况下,不会出现“关于作者”选项卡,这将非常有用。。。
提前感谢!
Edit: 根据WebElaine的建议,我尝试使用foreach
而不是print_r
:
function woo_new_product_tab_content() {
$terms = get_the_terms(get_the_ID(), \'book-author\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$all_term_meta = get_term_meta($term->term_id, \'wpcf-author-biography\',
false);
foreach($all_term_meta as $meta) {
echo $meta[0]; //tried with return as well with no success
}
}
}
}
这将产生以下结果:
<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">
G< </div>
如果我删除
[0]
之后
$meta
, 我们设法摆脱了丑陋的角色
Array
, 但我仍然有第二作者信息丢失的问题:
<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--author_tab panel
entry-content wc-tab" id="tab-author_tab" role="tabpanel" aria-
labelledby="tab-title-author_tab" style="display: block;">
XX staff writer <strong>John Doe</strong> has covered
everything from men’s style and grooming to food, drinks and travel. John
has also interviewed some of the biggest names in film, television and
music.
<div class="woocommerce-tabs wc-tabs-wrapper">
<div id="tab-test_tab" class="woocommerce-Tabs-panel woocommerce-Tabs-panel--
test_tab panel entry-content wc-tab" style="display: none;">
<strong>Jane Doe </strong>is an author and university lecturer in
underwater basket-weaving. She has recently completed a PhD.
</div>
</div> </div>