Skip to content

Commit

Permalink
Add support for page templates support in CPTs and posts
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 21, 2020
1 parent 434752c commit cd998d5
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

/**
* Load the page templates in Gutenberg.
*
* @param array $templates Theme page templates.
* @return array Modified templates.
*
* @param array $templates Page templates.
* @param WP_Theme $theme WP_Theme instance.
* @param WP_Post $post The post being edited, provided for context, or null.
* @return array (Maybe) modified page templates array.
*/
function gutenberg_load_fse_page_templates( $templates ) {
function gutenberg_load_fse_page_templates( $templates, $theme, $post ) {
if ( ! gutenberg_is_fse_theme() ) {
return $templates;
}
Expand All @@ -25,11 +27,16 @@ function gutenberg_load_fse_page_templates( $templates ) {
);
$page_templates = array();
if ( isset( $data['pageTemplates'] ) ) {
foreach ( $data['pageTemplates'] as $key => $page_emplate ) {
$page_templates[ $key ] = $page_emplate['title'];
foreach ( $data['pageTemplates'] as $key => $page_template ) {
if ( ( ! isset( $page_template['postTypes'] ) && 'page' === $post->post_type ) ||
( isset( $page_template['postTypes'] ) && in_array( $post->post_type, $page_template['postTypes'], true ) )
) {
$page_templates[ $key ] = $page_template['title'];
}
}
}

return $page_templates;
}
add_filter( 'theme_page_templates', 'gutenberg_load_fse_page_templates' );
add_filter( 'theme_post_templates', 'gutenberg_load_fse_page_templates', 10, 3 );
add_filter( 'theme_page_templates', 'gutenberg_load_fse_page_templates', 10, 3 );

0 comments on commit cd998d5

Please sign in to comment.