-
i have used a “numberOfItems”: “%numberofitems%”, “hasPart”: [ %haspart_json% ], on my schema data, rank math is not accepting, in my case, nothing happens when I add in custom code and click on the proceed code. I already added a PHP code to my website, here is the code // Hook into Rank Math’s schema generation to add dynamic numberOfItems and hasPart
add_filter( ‘rank_math/snippet/json_ld’, function( $json_ld, $data, $json_context ) {
if ( is_tag() || is_category() ) {
$term = get_queried_object();
$posts = get_posts( array(
‘post_type’ => ‘post’,
‘tax_query’ => array(
array(
‘taxonomy’ => $term->taxonomy,
‘field’ => ‘term_id’,
‘terms’ => $term->term_id,
),
),
‘posts_per_page’ => -1,
) );$itemList = array();
$position = 1;foreach ( $posts as $post ) {
setup_postdata( $post );
$itemList[] = array(
“@type” => “ListItem”,
“position” => $position,
“url” => get_permalink( $post ),
“name” => get_the_title( $post ),
“description” => get_the_excerpt( $post ),
“datePublished” => get_the_date( ‘c’, $post ),
“dateModified” => get_the_modified_date( ‘c’, $post ),
);
$position++;
}
wp_reset_postdata();// Define the dynamic numberOfItems and hasPart values
$numberOfItems = count( $posts );
$hasPart = array(
“@type” => “ItemList”,
“itemListElement” => $itemList
);// Inject dynamic data into the existing JSON-LD schema (modify the first schema entry)
if ( isset( $json_ld[0] ) && $json_ld[0][‘@type’] === ‘CollectionPage’ ) {
$json_ld[0][‘numberOfItems’] = $numberOfItems;
$json_ld[0][‘hasPart’] = $hasPart;
}
}return $json_ld;
}, 10, 3 );
The ticket ‘hasPart dynamic value insert error’ is closed to new replies.