Enabling Schema for custom post types and custom taxonomies

Need help with this?
Purchase All in One SEO Pack Pro to get dedicated support from us.

Buy Now

By default, All in One SEO Pack outputs Schema.org structured data markup for Posts, Pages, Categories, Tags and the search results page.  Users can enable Schema for Custom Post Types and Custom Taxonomies using the code below:

if ( ! function_exists( 'aioseop_schema_custom_post_types' ) ) {
     /**
      * @param array $layout
      * @return array
      */
     function aioseop_schema_custom_post_types( $layout ) {

          if ( is_archive() ) {
               if ( is_tax( 'taxonomy name' ) ) {
                    $layout = array(
                         '@context' => 'https://schema.org',
                         '@graph' => array(
                              '[aioseop_schema_Organization]',
                              '[aioseop_schema_WebSite]',
                              '[aioseop_schema_CollectionPage]'
                          ),
                    );
               }
          } elseif ( is_singular() || is_single() ) {
               global $post;

               if ( in_array( get_post_type( $post ), array( 'post type name' ) ) ) {
                    $layout = array(
                         '@context' => 'https://schema.org',
                         '@graph' => array(
                              '[aioseop_schema_Organization]',
                              '[aioseop_schema_WebSite]',
                          ),
                    );

                    array_push( $layout['@graph'], '[aioseop_schema_WebPage]' );
                    if ( ! is_post_type_hierarchical( $post->post_type ) ) {
                         array_push( $layout['@graph'], '[aioseop_schema_Article]' );
                         array_push( $layout['@graph'], '[aioseop_schema_Person]' );
                    }

               }
          }

          return $layout;
     }
     add_filter( 'aioseop_schema_layout', 'aioseop_schema_custom_post_types' );
}

In the example code above we’re outputting structured data markup for Organization, Website and Collection Page on terms in the Genres custom taxonomy and we’re outputting structured data markup for Organization, Website, WebPage, Article and Person on posts in the Movies custom post type.

You can change the output for custom post types from Article to WebPage by removing this line:

array_push( $layout[‘@graph’], ‘[aioseop_schema_Article]’ );

 

For instructions on how to use this code, please refer to our FAQ here.

  • Was this helpful ?
  • YesNo
[i]
[i]
Skip to toolbar