是否有WordPress插件将插件文件注册为自定义页面模板?


9

我需要创建一个插件,以使wp-admin中可以使用自定义页面模板。我想知道是否有人已经解决了这个问题,因为这似乎是一个非常典型的过程?


我不太明白这个问题。您想要一个插件来插入在插件中定义的自定义模板吗?
史蒂文

Answers:


7

就像Rarst回答的那样,您确实可以做到这一点,而无需编辑核心文件或删除页面属性metabox并使用相同的代码进行一些修改即可创建您的on。下面的代码是/admin/include/meta-boxes.php的代码,我添加了一条注释以显示您的附加页面模板选项的位置:

function page_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
    if ( $post_type_object->hierarchical ) {
        $pages = wp_dropdown_pages(array('post_type' => $post->post_type, 'exclude_tree' => $post->ID, 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
        if ( ! empty($pages) ) {
        ?>
        <p><strong><?php _e('Parent') ?></strong></p>
        <label class="screen-reader-text" for="parent_id"><?php _e('Parent') ?></label>
        <?php echo $pages; ?>
        <?php
        } // end empty pages check
    } // end hierarchical check.
    if ( 'page' == $post->post_type && 0 != count( get_page_templates() ) ) {
        $template = !empty($post->page_template) ? $post->page_template : false;
        ?>
        <p><strong><?php _e('Template') ?></strong></p>
        <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
        <option value='default'><?php _e('Default Template'); ?></option>
        <?php page_template_dropdown($template); ?>

        // add your page templates as options

        </select>
        <?php
    } ?>
    <p><strong><?php _e('Order') ?></strong></p>
    <p><label class="screen-reader-text" for="menu_order"><?php _e('Order') ?></label><input name="menu_order" type="text" size="4" id="menu_order" value="<?php echo esc_attr($post->menu_order) ?>" /></p>
    <p><?php if ( 'page' == $post->post_type ) _e( 'Need help? Use the Help tab in the upper right of your screen.' ); ?></p>
    <?php
}

不知道这是否能解决您的问题,但是当我需要在插件内置主题中显示帖子类型时就遇到了笑脸情况,为此我使用了add_filter('the_content', 'my-function'); my-function创建了要显示的输出。

另一个选择是让您的插件在当前主题目录中创建模板文件,如下所示:

function create_plugins_theme_file(){
    $file_name = TEMPLATEPATH . '/' . $tamplate_name . '.php';
    $handle = fopen($file_name, 'w') or wp_die('Cannot open file for editing');
    $file_contents = <<<OUT
<?php
/*
Template Name: $tamplate_name
*/
?>

//you theme file here

OUT;

   fwrite($handle, $file_contents);
   fclose($handle);
 }

您可以在首先检查文件是否存在后运行此文件

if(!file_exists( $file_name)){create_plugins_theme_file();}

希望其中之一能有所帮助。


将按照我的答案测试meta box的内容,总线-这只是问题的一部分,其他则是-仅在主题目录中尝试加载模板的代码。因此,提供“外部”模板文件可能不足以加载它。
罗斯特2011年

这就是为什么我建议让插件在主题目录中创建文件的原因。
Bainternet

2

我不完全确定我了解您要实现的目标,至少是为什么您希望插件做到这一点。

创建不同页面模板的正常过程是:

  1. 在您的ACTIVE主题目录中创建一个新的页面模板(复制page.php)。

  2. 更改模板的名称(在文件内部)。

    / *模板名称:全宽页面* /

  3. 将页面的代码更改为您要实现的代码。

  4. 现在,您可以创建一个新页面,然后选择要使用的“模板”。

替代文字

...

我希望这是您要实现的目标?

此处的官方文档:http : //codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates


哎呀 示例PHP代码有点混乱。访问链接以获取正确的代码示例。
拉尔斯·库达

2
在明知有兴趣,如果任何人使用在现有的插件简的方法:wordpress.stackexchange.com/questions/3396/...
jnthnclrk

我相信@trnsfrmr正在寻找一种方法,如何在他自己的插件中创建预定义页面模板(步骤1-3),以使该模板在激活插件后可供用户使用。我想他想让这个解决方案主题保持独立。
Michal Mau

啊,好吧,我在那儿误会了一点,谢谢您的澄清:-)
Lars Koudal 2011年

2

这似乎很难实现。get_page_templates()函数主动丢弃父级和子级根目录中未包含的任何内容。它还不存储在全局变量中,也不允许过滤生成的模板列表。

我认为页面属性元框将需要分叉,并为此完全替换。并且不确定即使那样也有可能。

我同意这似乎是有道理的,但是WordPress代码非常精确,因为它希望命名模板仅来自主题目录。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.