最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

themes - Adding a Template to the Page Attributes Panel for both Posts and Pages?

programmeradmin6浏览0评论

I would like to keep my main theme as the active theme, but allow users to select a slightly different layout via the "Page/Post Attributes" panel. Ideally, I'd like to store this layout under my main theme's "styles" directory, in its own folder.

MyTheme > styles > My-special-Layout > style.css

So that in the "Page Attributes" panel, I see a template there called "My-special-layout"...

However, I have two issues...

  1. I can't seem to get the "child" theme to appear in the "Page Attributes" panel. (I'm simply adding a folder under my main theme directory and placing a style.css file there that has the value "Template: my_main_theme_directory"). But I never see any templates appear in the "page attributes" panel.

  2. I can't get the "Page Attributes" panel on the POST editor. I'd like to allow the template to be applied to Posts as well as Pages. How to get this panel on the post editor?

I would like to keep my main theme as the active theme, but allow users to select a slightly different layout via the "Page/Post Attributes" panel. Ideally, I'd like to store this layout under my main theme's "styles" directory, in its own folder.

MyTheme > styles > My-special-Layout > style.css

So that in the "Page Attributes" panel, I see a template there called "My-special-layout"...

However, I have two issues...

  1. I can't seem to get the "child" theme to appear in the "Page Attributes" panel. (I'm simply adding a folder under my main theme directory and placing a style.css file there that has the value "Template: my_main_theme_directory"). But I never see any templates appear in the "page attributes" panel.

  2. I can't get the "Page Attributes" panel on the POST editor. I'd like to allow the template to be applied to Posts as well as Pages. How to get this panel on the post editor?

Share Improve this question edited Sep 10, 2010 at 4:57 MikeSchinkel 37.6k14 gold badges117 silver badges132 bronze badges asked Sep 9, 2010 at 12:54 Scott BScott B 5,69614 gold badges94 silver badges148 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

You're not doing child themes right. A child theme is a separate theme altogether that everyone must use, but relies on another theme for all template parts it doesn't provide. What you want is templates:

http://codex.wordpress/Theme_Development#Defining_Custom_Templates

Basically, just create a new theme file in the theme's root directory (e.g. foobar.php) write this at the top:

/*
Template Name: Foobar
*/

That will give you a new template called Foobar (obviously, change Foobar to whatever you want. That's the name that will appear in the dropdown menu on the editor page).

As of now, WordPress only supports templates for pages and custom post types, not posts. There are ways to hack this, like checking for a post meta on posts and pulling it on template include:

function my_post_templater($template){
  if( !is_single() )
    return $template;
  global $wp_query;
  $c_template = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
  return empty( $c_template ) ? $template : $c_template;
}

add_filter( 'template_include', 'my_post_templater' );

function give_my_posts_templates(){
  add_post_type_support( 'post', 'page-attributes' );
}

add_action( 'init', 'give_my_posts_templates' );

If you put that code in your theme's functions.php file, that should work (as long as you actually have custom templates in your theme folder).

For more information about child themes, read here:

http://codex.wordpress/Child_Themes

From wordpress 4.7+ you can put 'Template Post Type' in your custom template file to display it in page attribute for both page and post.

/*
  Template Name: Your Template Name
  Template Post Type: post, page
*/
发布评论

评论列表(0)

  1. 暂无评论