hasPart dynamic value insert error

#896046
  • Resolved Santosh Sendha
    Rank Math free

    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 );

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    Can you please confirm if you’re trying to use any custom variable in the Rank Math schema? If so, please note that the variables outside of Rank Math will not work on the schema.

    However, if you want, you can use this filter on your website to create a custom variable for Rank Math: https://rankmath.com/kb/variables-in-seo-title-description/#how-to-add-custom-variables

    Hope that helps and please don’t hesitate to let us know if you have any other questions.
     
    Thank you.

    I want to dynamically update the numberOfItems, and the hasPart section, how do I do it?

    Hello,

    It seems that you’re already doing that using the code you initially shared so there’s no need to use any variables.

    Can you please let us know what kind of error you see from your end if you use the mentioned code?

    Looking forward to helping you.

    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 3 replies - 1 through 3 (of 3 total)

The ticket ‘hasPart dynamic value insert error’ is closed to new replies.