Automatic display of related products in articles according to tags

Automatic display of related products in articles according to tags

In order to automatically display WooCommerce products in related articles, just define a label for the products and articles so that the products are automatically displayed at the end of the list of articles.

To activate this system, just add the following code to the end of the function file and add the tags you want, note that you have no restrictions on adding various tags.

 


   


add_filter( 'the_content', 'Rezvan_blog_post_related_products' );
 
function Rezvan_blog_post_related_products( $text ) {
   if ( is_singular( 'post' ) ) {
      $word_to_tag = array(
         'bag' => 'bag',
         'shoes' => 'shoes',
         'accessory' => 'accessory',
	'mobile' => 'mobile',
      );
      $max = 0;
      $top_tag = '';
      foreach ( $word_to_tag as $word => $tag_slug ) {
         if ( substr_count( $text, $word ) > $max ) {
            $max = substr_count( $text, $word );
            $top_tag = $tag_slug;
         }
      }
      if ( $top_tag ) $text .= '<h2>Related Products</h2>' . do_shortcode( '
' ); } return $text; }

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen + 15 =

 
Online-Chat

We are ready to answer your questions