php - How do I pass product title to a link


I can get the product title with $string = $product->get_title(); $product_title = urlencode($string); //to get rid of spaces

But how to I add that $string to the middle of a url to create$product_title/#review

I am trying to use it here (function.php) in this code modified from Display blank(0) star ratings in the Woocommerce single product pages

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 ?>

To get the "(# Customer Reviews)" text to link to$product_title/#review in my product archive.

enter image description here


urlencode doesn't work because it adds underscores, and the permalink structure for the slug uses hyphens. So now I have:

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

This code links to the product page and opens the reviews tab, but it does not stay on the anchor and instead jumps to the bottom of the product page.

How do I get it to stay.

People are also looking for solutions to the problem: PHP different locations for images and thumbs


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.