选择自定义帖子类型的模板菜单?

发布于 2024-11-01 02:24:39 字数 732 浏览 0 评论 0原文

我在 WordPress 中创建了几个自定义帖子类型,并将 hierarchy 设置为 true,因此它的行为就像一个页面。

问题是,模板选择不可用。我已经应用了这个黑客 让菜单出现:

里面有一个文件meta-boxes.php WordPress 的 wp-admin\includes 安装..文件的第547行, 这是函数 page_attributes_meta_box() 只需添加 您的特定职位的支票 输入名称即可显示 模板页面下拉。

if (('page' == $post->post_type || 'yourcustomposttype' == $post->post_type) && 0 != count( get_page_templates() ) ) {
        $template = !empty($post->page_template) ? $post->page_template : false;
        ?>

这成功地使菜单出现,但数据不会保存。 “父”部分会保存,但“模板”不会保存。

有人有什么想法吗?

I have created a couple of custom post types in wordpress, and have set hierarchical to true so it behaves as a page.

The problem is, templates selection isn't available. I've applied this hack to get the menu to appear:

there is a file meta-boxes.php within
wp-admin\includes of the wordpress
installation.. line 547 of the file,
which is the function
page_attributes_meta_box() just add
the check for your particular post
type name to be able to display the
template pages drop down.

if ( ('page' == $post->post_type  || 'yourcustomposttype' == $post->post_type) && 0 != count( get_page_templates() ) ) {
        $template = !empty($post->page_template) ? $post->page_template : false;
        ?>

This successfully makes the menu appear, but the data won't save. The "parent" section saves but the "template" doesn't.

Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慢慢从新开始 2024-11-08 02:24:39

此功能自 4.7 起内置于核心中。启用应用程序自定义模板内的功能。例如,如果您有一个名为“Promotions”的模板,并且希望将其应用到您的 CPT“usa、uk、au”

<?php 
  /* 
    Template Name: Promotions
    Template Post Type: page, usa, uk, au
  */

get_header(); ?>

WordPress 4.7 为所有帖子类型带来自定义页面模板功能

This feature is built into core since 4.7. Enable the functionality inside the applicate custom template. For example, if you have a template called "Promotions" and you want to apply it to your CPT "usa, uk, au"

<?php 
  /* 
    Template Name: Promotions
    Template Post Type: page, usa, uk, au
  */

get_header(); ?>

WordPress 4.7 Brings Custom Page Template Functionality to All Post Types

清风疏影 2024-11-08 02:24:39

我用过这个插件,发帖没问题,试试吧:)

https:// /wordpress.org/extend/plugins/custom-post-template/

I've used this plugin and it was ok for posts, try it :)

https://wordpress.org/extend/plugins/custom-post-template/

方圜几里 2024-11-08 02:24:39

偶然发现这个问题寻找相同的功能。我知道这有点晚了,但我想我找到了解决方案。通过使用该插件查看自述文件,您可以将其添加到您正在寻找的功能的functions.php中。

/**
 * Hooks the WP cpt_post_types filter 
 *
 * @param array $post_types An array of post type names that the templates be used by
 * @return array The array of post type names that the templates be used by
 **/
function my_cpt_post_types( $post_types ) {
    $post_types[] = 'movie';
    $post_types[] = 'actor';
    return $post_types;
}
add_filter( 'cpt_post_types', 'my_cpt_post_types' );

Stumbled across this question looking for the same functionality. I know this is a little late but I think I found the solution. By looking into the readme file with the plugin you can add this to your functions.php for the functionality you're looking for.

/**
 * Hooks the WP cpt_post_types filter 
 *
 * @param array $post_types An array of post type names that the templates be used by
 * @return array The array of post type names that the templates be used by
 **/
function my_cpt_post_types( $post_types ) {
    $post_types[] = 'movie';
    $post_types[] = 'actor';
    return $post_types;
}
add_filter( 'cpt_post_types', 'my_cpt_post_types' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文