如何将产品标题传递给链接

时间:2020-06-25 16:06:07

标签: woocommerce

我可以得到产品标题 $ string = $ product-> get_title(); $ product_title = urlencode($ string); //摆脱空间

但是如何将$ string添加到url的中间以创建 http://example.com/product/$product_title/#review

我试图在此代码中使用它(function.php),该代码是从Woocommerce单个产品页面中的Display blank(0)星级评定而修改的

add_shortcode ( 'show_product_ratings_count', 'add_count_to_reviews_tab_item', 98 );

function add_count_to_reviews_tab_item( $title ) {
    global $product;

    $rating_count = $product->get_rating_count();

    if ( $rating_count >= 0 ) {
        $title = $product->get_title();
        $review_count = $product->get_review_count();
        $average      = $product->get_average_rating();
        $count_html   = '<div class="count-rating">' . array_sum($product->get_rating_counts()) . '</div>';
        ?>
        <div class="woocommerce-product-rating-single">

            <?php if ( comments_open() ) : ?>
            <a href="#reviews" class="woocommerce-review-link" rel="nofollow">
                (<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); 
            ?>)</a><?php endif ?>
            
        </div>
        <?php
    }
}

要获取“(#客户评论)”文本以链接到我的产品档案中的http://example.com/product/$product_title/#review

enter image description here

更新

urlencode不起作用,因为它添加了下划线,并且该段的永久链接结构使用连字符。所以现在我有:

global $product
$product_slug = $product->get_slug();
$url_to_review = $product_slug . "/#reviews"; //or #tab-reviews, to append the review anchor
<a href= "/product/<?php echo $url_to_review; ?>";

此代码链接到产品页面并打开“评论”标签,但它不停留在锚点上,而是跳转到产品页面的底部。

我如何保持它。

0 个答案:

没有答案