Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Changes the schema type based on the current page category
- * In this example, we are using "CreativeWork" as the default custom schema type in the settings
- * Paste this in your active theme's functions.php file.
- * @param array $schema
- * @return array
- */
- add_filter('site-reviews/schema/CreativeWork', function ($schema, $args) {
- $taxonomy = 'category'; // Change this if you are using a custom taxonomy
- $mappedSchemaTypes = [ // [category name] => [schema type]
- 'Books' => 'Book',
- 'Movies' => 'Movie',
- 'Podcast' => 'PodcastSeries',
- 'TV Series' => 'TVSeries',
- 'Video Games' => 'VideoGame',
- ];
- if (is_array($categories = get_the_terms(get_the_ID(), $taxonomy))) {
- foreach ($categories as $category) {
- if (!array_key_exists($category->name, $mappedSchemaTypes)) {
- continue;
- }
- $schemaType = $mappedSchemaTypes[$category->name];
- $schema['@type'] = $schemaType;
- $schema = apply_filters('site-reviews/schema/'.$schemaType, $schema, $args);
- break;
- }
- }
- if (is_wp_error($categories)) {
- glsr_log()->error($categories->get_error_message());
- }
- return $schema;
- }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement