首页 > 解决方案 > 在 WooCommerce 产品描述周围添加一个 div

问题描述

我使用以下方法在单个产品页面上移动了产品摘要:

add_action( 'woocommerce_single_product_summary', 'custom_the_content', 35 );

function custom_the_content() {
echo the_content();
}

现在我需要将内容包装在一个div中,所以我尝试了

    echo '<div class="mydiv">' . the_content() . '</div>';

但这导致内容没有被 div 包裹,而是 div 是空的,只是添加到 html 中,内容如下。如何将内容包装到我的 div 中?

标签: phpwordpresswoocommerce

解决方案


您需要使用 get_the_content(); 而不是 the_content(); 如果你想自己回应它。

the_content() 已经在内部显,因此您将无法包装它。

如果您的页面内容包含应由 wp 内部过滤器(如简码)处理的数据,那么您应该使用如下内容:

ob_start();
the_content();
$content= ob_get_clean();
echo '<div class="mydiv">' . $content. '</div>';

更多解释:WordPress:get_the_content() 和 the_content() 之间的区别


推荐阅读