Php 如何在所有页面(而不仅仅是商店页面)上添加星级和计数器?

Php 如何在所有页面(而不仅仅是商店页面)上添加星级和计数器?,php,wordpress,woocommerce,wordpress-theming,custom-wordpress-pages,Php,Wordpress,Woocommerce,Wordpress Theming,Custom Wordpress Pages,我试图添加平均明星收视率和一个计数器旁边显示有多少评论已经离开。我只在我的商店页面上使用它,通过将以下代码添加到我的themes(sober)functions.php文件中实现了这一点: add_action( 'woocommerce_after_shop_loop_item_title', 'gkartikey_woocommerce_template_loop_rating', 5 ); if ( ! function_exists( 'gkartikey_woocommerce_tem

我试图添加平均明星收视率和一个计数器旁边显示有多少评论已经离开。我只在我的商店页面上使用它,通过将以下代码添加到我的themes(sober)functions.php文件中实现了这一点:

add_action( 'woocommerce_after_shop_loop_item_title', 'gkartikey_woocommerce_template_loop_rating', 5 );
if ( ! function_exists( 'gkartikey_woocommerce_template_loop_rating' ) ) {

    /**
     * Display the average rating in the loop.
     */
    function gkartikey_woocommerce_template_loop_rating() {
        wc_get_template( 'loop/rating.php' );
    }
}


add_filter( 'woocommerce_product_get_rating_html', function ( $html, $rating, $count ) {
    global $product;
    if ( $html && is_archive() && $product) {
        $html .= sprintf( '<span>%s</span>', $product->get_rating_count() );
    }

    return $html;
}, 10, 3 );
add_action('woocommerce_在_shop_loop_item_title'之后,'gkartike_woocommerce_模板_loop_rating',5);
如果(!function_存在('gkartekey_-woocommerce_-template_-loop_-rating')){
/**
*在循环中显示平均额定值。
*/
功能GKARTEKEY\U WOOMECUSE\U模板\U循环\U评级(){
wc_get_模板('loop/rating.php');
}
}
添加过滤器('woocommerce\u product\u get\u rating\u html',函数($html,$rating,$count){
全球$产品;
if($html&&is_archive()&&&product){
$html.=sprintf('%s',$product->get_rating_count());
}
返回$html;
}, 10, 3 );
问题是计数器不会出现在任何其他页面上,比如我的主页,我在那里使用Elementor添加了产品

示例图片


示例图片

什么是Elementor?也许你可以把它联系起来,澄清一下Elementor是一个页面生成器@Nyov只有当它是存档文件时,才将其添加到页面中。删除
is\u archive()
看看效果如何。没有经过测试,只是符合逻辑。