• Resolved shoufu

    (@shoufu)


    The Theme I use for Woocommerce is OPSTORE LITE,
    I have set 20-50 tags for each product, All of these tags are display on product page by Default,I would like to display rand 10 tags.

    After searching on WOO-Document and Google,I found someone share his experience but it is for cloud-widget-tag by changing “orderby–>rand”.
    I can not find any about how to display random 10 (product tags) for product page.Can anyone help me?
    Tks

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hi

    1- Try adding this function to your active theme’s/child theme’s functions.php file:

    function ywp_get_product_tag_list( $id, $before = '', $sep = '', $after = '' ) {
        $terms = get_the_terms( $id, 'product_tag' );
    
        if ( is_wp_error( $terms ) ) return $terms;
    
    	if ( empty( $terms ) ) return false;
    
    	// Randomize Term Array
    	shuffle( $terms );
    
    	// Grab Indices 0 - 9, 10 in total
    	$random_terms = array_slice( $terms, 0, 9 );
    
    	$links = array();
    
    	foreach ( $terms as $term ) {
    		$link = get_term_link( $term, $taxonomy );
    
    		if ( is_wp_error( $link ) ) return $link;
    			$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
    	}
    
    	$term_links = apply_filters( "term_links-{$taxonomy}", $links );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
    	
    	return $before . join( $sep, $term_links ) . $after;
    }

    2- Go to:

    wp-content/themes/opstore/woocommerce/single-product/meta.php

    3- Replace:

    <?php echo wc_get_product_tag_list( $product->get_id(), ', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', count( $product->get_tag_ids() ), 'woocommerce' ) . ' ', '</span>' ); ?>

    to:

    <?php echo ywp_get_product_tag_list( $product->get_id(), ', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', count( $product->get_tag_ids() ), 'woocommerce' ) . ' ', '</span>' ); ?>

    Good luck

Viewing 1 replies (of 1 total)
  • The topic ‘How to display random limit numbers of (product tags) on product page’ is closed to new replies.