Using the Section Widget on Custom Post pages
-
I use the Section Widget for all our side bar content, but recently we introduced a custom post type, and needed some widgets displayed on only those pages, so I modified the widget to support custom post types. The changes are quite simple, and so I will post them here.
Only one file needs to be modified. That file is
olt-checklist\olt-checklist.php.The following function needs to be added somewhere in the file. I added it just after the line at about line 9 that reads:
if(!function_exists('olt_checklist')) {. The function is:/* * PSV: Function to get custom post types as an array */ function getCustomPostTypes() { $args = array('_builtin' => false); $output = 'names'; // names or objects, note names is the default $operator = 'and'; // 'and' or 'or' $post_types = get_post_types($args, $output, $operator); return $post_types; }That simply returns an array of the defined custom post types. Next, we need to add the post types to the array of condition check boxes:
/* PSV: Add all custom post types */ $post_types = getCustomPostTypes(); foreach ($post_types as $post_type) { $type = get_post_type_object($post_type); array_push($data, (object) array('id' => $post_type, 'name' => $type->labels->name, 'description' => 'Display this widget on all '.$type->labels->name.'.')); }That should be added just before the line that reads:
$walker = new OLTSpecialPagesChecklistWalker;Next, we need to add to the condition check, so we know when to display the widget. Just after the code that reads:
case "notfound": if(is_404()) return true; break;add the following:
default: // PSV: allow for custom post types if(is_singular($key)) return true; break;That’s it. With these changes, you will see new check boxes for your custom post types at the bottom of the Special Pages tab.
The topic ‘Using the Section Widget on Custom Post pages’ is closed to new replies.