【问题标题】:Wordpress: Page template with default fieldsWordpress:具有默认字段的页面模板
【发布时间】:2012-01-09 02:01:21
【问题描述】:

WordPress 3.2.1

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

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

等等

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

谢谢大家!

【问题讨论】:

  • 您希望这些字段在前端填写还是由管理员填写以使值显示在前端?
  • 管理员将在 WordPress 管理区域中填写这些值。我只是希望模板根据正在创建/编辑的模板自动显示可用的字段。

标签: wordpress


【解决方案1】:

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

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

的 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);
    }
}

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多