Home / Admin / Change Schema Type of All Posts in Specific Categories
Duplicate Snippet

Embed Snippet on Your Site

Change Schema Type of All Posts in Specific Categories

This snippet changes the 'NewsArticle' schema of all posts under specific categories to 'Article' schema.

Note:
1. Please replace 'category1-slug', 'category2-slug', and 'category3-slug' with the actual slugs of the categories.
2. You can add more categories by adding a comma-separated value in the $target_category_slugs array.

100+
Code Preview
php
<?php
add_filter('aioseo_schema_output', 'change_schema_type_in_specific_categories');
function change_schema_type_in_specific_categories($schema) {
    // Specify the category slugs you want to target
    $target_category_slugs = array('category1-slug', 'category2-slug', 'category3-slug');
    foreach ($schema as &$schemaItem) {
        // Check if the post belongs to any of the specified categories
        if (isset($schemaItem['@type']) && 'NewsArticle' === $schemaItem['@type'] && has_category($target_category_slugs)) {
            $schemaItem['@type'] = 'Article';
			unset($schemaItem['dateline']);
        }
    }
    return $schema;
}

Comments

Add a Comment