在自定义元框中发布摘录...帮助了解正确的php格式

时间:2011-02-26 作者:m-torin

我有一个定制的元框,附在一个帖子类型上,显示客户提交新想法的方式。客户应该用30个字符或更少的字符来总结他们的想法,这似乎是post\\u摘录的一个很好的用途。我对php不太在行(仍在学习中),因此如果您能帮助我正确使用echo语句,我将不胜感激。

下面是我的自定义元框。为了简单起见,我去掉了其他一切。

帖子摘录将显示在元框中,但任何编辑都不会保存在元框中。我做错了什么?

function idea_information() {
    global $post;

    // Noncename needed to verify where the data originated
    echo \'<input type="hidden" name="ideameta_noncename" id="ideameta_noncename" value="\' .
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    // Get the location data if its already been entered
    $quicknote = get_post_meta($post->ID, \'_ideas_quicknote\', true);


    // Echo out the fields
    echo \'<h3>Basics</h3>
          <input type="text" name="excerpt" id="excerpt" value="\' . $post->post_excerpt  . \'"/>\';

}

// Save the Metabox Data

function whiteout_save_idea_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST[\'ideameta_noncename\'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( \'edit_post\', $post->ID ))
        return $post->ID;

    // OK, we\'re authenticated: we need to find and save the data
    // We\'ll put it into an array to make it easier to loop though.
    $station_meta[\'_ideas_quicknote\'] = $_POST[\'_ideas_quicknote\'];

    // Add values of $station_meta as custom fields

    foreach ($station_meta as $key => $value) { // Cycle through the $station_meta array!
        if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
        $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn\'t have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

}

add_action(\'save_post\', \'whiteout_save_idea_meta\', 1, 2); // save the custom fields
相关岗位:Replace the Post Excerpt Meta Box with a Field in My Custom Meta Box

1 个回复
最合适的回答,由SO网友:mfields 整理而成

您似乎要打印的唯一自定义表单字段是名为“摘录”的文本输入。在处理器脚本中,您正在检查名为“\\u ideas\\u quicknote”的$\\u POST var。由于它实际上没有在POST请求中发送,因此处理器将永远看不到它。请尝试以下操作:

<?php
print "\\n" . \'<h3>Basics</h3>\';
print "\\n" . \'<input type="text" name="_ideas_quicknote" id="myplugin_ideas_quicknote" value="\' . esc_attr( $quicknote )  . \'"/>\';

结束

相关推荐

Automating Excerpt

我正在努力automate 编辑通过自动化工作excerpts.我的解决方案可行,但几乎没有问题:如果一篇文章开头有图像/破坏的html,它会破坏版面。子字符串剪切单词。是否有更好的解决方案来自动化摘录或改进现有代码?<?php if(!empty($post->post_excerpt)) { the_excerpt(); } else { echo \"<p>\".substr(get_the