是否在自定义WooCommerce选项卡中输出一组术语元值?

时间:2017-05-17 作者:ninox boobook

请原谅,我的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] =&gt; XX staff writer&nbsp;<strong>John Doe</strong>&nbsp;has covered 
everything from men’s&nbsp;style and grooming to food, drinks and travel. John 
has also interviewed&nbsp;some of the biggest names in film, television and 
music.
)
Array
(
[0] =&gt; <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&nbsp;</strong>is an author and university 
lecturer&nbsp;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&lt;           </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&nbsp;<strong>John Doe</strong>&nbsp;has covered 
everything from men’s&nbsp;style and grooming to food, drinks and travel. John 
has also interviewed&nbsp;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&nbsp;</strong>is an author and university lecturer&nbsp;in 
underwater basket-weaving. She has recently completed a PhD.

</div>
</div>          </div> 

2 个回复
SO网友:WebElaine

用于作者内容显示,而不是print_r($all_term_meta); 尝试foreach:

foreach($all_term_meta as $meta) {
    return $meta[0];
}
您可能需要使用“echo”而不是“return”,但请先尝试“return”。

要控制是否有“作者”选项卡,请更新woo_new_product_tab 具有相同条件的功能:

function woo_new_product_tab( $tabs ) {
    $terms = get_the_terms(get_the_ID(), \'book-author\' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        // Adds the new tab
        $tabs[\'author_tab\'] = array(
            \'title\'     => __( \'About the author\', \'woocommerce\' ),
            \'priority\'  => 50,
            \'callback\'  => \'woo_new_product_tab_content\'
        );
    }
    return $tabs;
}

SO网友:ninox boobook

由于WebElaine,我设法找到了解决隐藏选项卡问题的解决方案:

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 \'<p>\' . strip_tags($meta, \'<strong><p><br>\') . \'</p>\';
    }
   }
  }
}
这将输出以下令人满意的HTML:

<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;">

   <p>XX staff writer&nbsp;<strong>John Doe</strong>&nbsp;has 
   covered everything from men’s&nbsp;style and grooming to food, drinks and 
   travel. John has also interviewed&nbsp;some of the biggest names in film, 
   television and music.</p>

   <p><strong>Jane Doe&nbsp;</strong>is an author and university 
   lecturer&nbsp;in underwater basket-weaving. She has recently completed a 
   PhD.</p>         

</div>

结束

相关推荐

减少/优化生成固定链接时wp_get_Object_Terms()的调用

“我的主题”运行一个循环,不需要post术语来显示。然而,Wordpress core在每次迭代中都会运行wp\\u get\\u object\\u terms函数。这会导致不必要的DB查询,例如:SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr