How to display a widget on specific post types

felix lam 296394

This is a pretty simple method for ensuring your custom widget only displays on specific post types. You can find guidance on creating new widgets at https://codex.wordpress.org/Widgets_API.

To ensure your widget only displays for specified post types, update the widget function with the following snippet:

public function widget( $args, $instance ) {
  if ( 'artist' != get_post_type() ) { // Change this post type as required
    return;
  }
  // Output the content of the widget here
}

All this does is check the post type of the current page then bails before any content is output if the post type doesn’t match the required post type. You can add the rest of the function after.

It’s very simple.

Leave a Reply

Your email address will not be published. All fields are required.