Auto pulling name and url for ItemList

#917186
  • Resolved Brady
    Rank Math pro
    Content AI Starter

    We have many categories and sub categories along with thousands of products on our website. Is there a way to set up the custom schema for item list so that it defaults to the name in the URL for all the products that are in the category? If so, if new products are added to those categories, will that schema automatically update?

    Also, if this is possible, is it also possible for the list item schema?

Viewing 1 replies (of 1 total)
  • Hello,

    Thank you for contacting Rank Math support.

    You can use the following filter to add the itemList Schema with details of each product to the category pages:

    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
    	if(is_product_category()) {
    		$products = get_posts( array(
      			'post_type' => 'product',
      			'numberposts' => -1,
      			'post_status' => 'publish',
      			'fields' => 'ids',
      			'tax_query' => array(
        	array(
            	'taxonomy' => 'product_cat',
            	'field' => 'id',
            	'terms' => get_queried_object_id(),
            )
         ),
      ));
    		$data['itemList']['itemListElement'] = [];
    		foreach($products as $key => $product) {
    			$url = get_the_permalink($product);
    			$schema[] = [ "@type"=> "ListItem",
      				   "position"=> $key,
    				   "item" => [
    						 "@type" => "Product",
    						 "name" => get_the_title($product),
    					     "url"=> $url,
    					     "image" => get_the_post_thumbnail_url($product, 'full'),
    						 "offers" => [
    							 "@type" => "offer",
    							 "priceCurrency" => "PKR",
    							 "price" => wc_get_product($product)->get_price()
    							 
    						 ],
    					 ]
    	  			  ];
    		$data['itemList']['@type'] = 'itemList';
    		$data['itemList']['itemListElement'] = $schema;
    	}
    	}
    	return $data;
    }, 99, 2);

    Here is how to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘Auto pulling name and url for ItemList’ is closed to new replies.