Wordpress:具有默认字段的页面模板

时间:2012-01-09 02:01:21

标签: wordpress

WordPress 3.2.1

我正在创建单独的页面模板,但是,有些页面模板应该具有默认的自定义字段。也就是说,如果有人创建了一个模板类型为“联系我们”的新页面,则默认情况下应该具有自定义字段:

成功消息 发邮件给 电话号码

现在,我能想到的唯一方法是将管理员添加这些自定义字段添加到页面中,然后填写它们。但是,这不是最好的基本上给管理员一个“交钥匙”类型的功能的方法,即他们创建一个具有特定模板的页面,只需填写字段。

谢谢你们!

1 个答案:

答案 0 :(得分:2)

以下内容将为您的(插入 - 编辑)页面添加1个额外的元框 在其中,您可以添加要保存的自定义字段。

根据所选模板,它不会切换 我建议使用javascript来显示/隐藏字段来执行此操作。基于< p>

的ID

如果您使用所有可能字段的元框,您可以显示我愿意帮助使用javascript来隐藏和显示基于模板。

GL

<?php    
add_action( 'add_meta_boxes', 'carrousel_build' );
function carrousel_build()
{
    add_meta_box('carrousel', 'Carrousel opties', 'carrousel_options', 'page',/*show on 'pages'*/ 'normal', 'high');
}

//this will add a meta box with custom fields
function carrousel_options ($post)
{ ?>
<p>
    Description
</p>
<p id="field1_container">
    <label for="field1">Custom field 1</label>
    <br/>
    <input type="text" id="field1" name="field1" value="<?php echo get_post_meta($post->ID, 'field1', true) ?>" size="25" />
</p>
<p id="field2_container">
    <label for="field2">Custom field 2</label>
    <br/>
    <input type="text" id="field2" name="field2" value="<?php echo get_post_meta($post->ID, 'field2', true) ?>" size="25" />
</p>

<?php }

//save the values of the meta box
add_action( 'save_post', 'post_save' );
function post_save($post_id)
{
    // Check permissions
    if (isset ($_POST['post_type']) && 'carrousel' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ))
    {
        return;
    }

    if (isset($_POST['field1'])) {
        $subtitle = $_POST['field1'];
        update_post_meta($post_id, 'field1', $subtitle);
    }

    if (isset($_POST['field2']))
    {
        $link = $_POST['field2'];
        update_post_meta($post_id, 'field2', $link);
    }
}