将 nl2br 添加到我的 WordPress 自定义元框
好吧,让我看看我能否正确解释这一点。在 WordPress 中,我们有一个用于插入摘录的框。我们需要添加第二个摘录框。我没有手动向每个帖子添加自定义字段,而是在管理帖子页面上放置了一个功能,以元框的形式自动添加自定义字段。
好吧,这就是我遇到的问题,这个函数正在工作,除了以下事实:无论您在该字段中输入什么,它都会丢失换行符。因此,当我们的作者为了保持文本块的格式而对此字段做出贡献时,我必须手动将
添加到段落末尾。
这是我的代码:
function my_create_post_meta_box() {
add_meta_box( 'my-meta-box', 'Second Excerpt', 'my_post_meta_box', 'post', 'normal', 'high' );
}
function my_post_meta_box( $object, $box ) { ?>
<p>
<label for="second-excerpt">
<strong>Second Excerpt With Images for Post List Page</strong>
</label>
<textarea name="second-excerpt" id="second-excerpt" cols="60" rows="4" tabindex="30" style="width: 97%;" wrap="hard"><?php echo wp_specialchars( get_post_meta( $object->ID, 'Second Excerpt', true ), 1 ); ?></textarea>
<input type="hidden" name="my_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php
}
function my_save_post_meta_box( $post_id, $post ) {
if ( !wp_verify_nonce( $_POST['my_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
$meta_value = get_post_meta( $post_id, 'Second Excerpt', true );
$new_meta_value = stripslashes( $_POST['second-excerpt'] );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, 'Second Excerpt', $new_meta_value, true );
elseif ( $new_meta_value != $meta_value )
update_post_meta( $post_id, 'Second Excerpt', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, 'Second Excerpt', $meta_value );
}
谢谢,任何帮助都可以。
Okay let me see if I can explain this right. In wordpress we have a box to insert an excerpt. We need to add a second excerpt box. Instead of manually adding a custom field to every post I have placed a function to automatically add a custom field in the form of a Meta box on the admin post page.
Okay so this is the problem that i'm having this function is working except for the fact that whatever you enter into this field it loses it's line breaks. So when our writers are contrubuting to this field in order to keep formatting of the block of text I have to manually add
to the end of the paragraph.
Here is my code:
function my_create_post_meta_box() {
add_meta_box( 'my-meta-box', 'Second Excerpt', 'my_post_meta_box', 'post', 'normal', 'high' );
}
function my_post_meta_box( $object, $box ) { ?>
<p>
<label for="second-excerpt">
<strong>Second Excerpt With Images for Post List Page</strong>
</label>
<textarea name="second-excerpt" id="second-excerpt" cols="60" rows="4" tabindex="30" style="width: 97%;" wrap="hard"><?php echo wp_specialchars( get_post_meta( $object->ID, 'Second Excerpt', true ), 1 ); ?></textarea>
<input type="hidden" name="my_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php
}
function my_save_post_meta_box( $post_id, $post ) {
if ( !wp_verify_nonce( $_POST['my_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
$meta_value = get_post_meta( $post_id, 'Second Excerpt', true );
$new_meta_value = stripslashes( $_POST['second-excerpt'] );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, 'Second Excerpt', $new_meta_value, true );
elseif ( $new_meta_value != $meta_value )
update_post_meta( $post_id, 'Second Excerpt', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, 'Second Excerpt', $meta_value );
}
Thanks and any help would do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在前端模板上使用 wpaautop 函数。喜欢:
Use wpautop function on frontend template. Like:
只需在
$new_meta_value = ...
之后添加此行:不要将您的值与
''
进行比较,最好使用empty()
。另外,有些比较是不必要的。因此,保存函数的添加/更新/删除部分可以这样编写:请注意,即使您的语句只有一行长,也始终建议使用大括号。它提高了可读性,并且如果/当您必须向 if 子句添加另一行时,不会弄乱事情。
Just add this line after
$new_meta_value = ...
:And instead of comparing your values to
''
, it's better to useempty()
. Also some of the comparisons are unneeded. Thus, the add/update/delete part of your save function can be written like this:Note that it's always advisable to use curly braces even if your statement is only one row long. It improves readability and doesn't mess things up if/when you have to add another row to the if clause.
不要修改保存到数据库的数据。准确保存用户输入的内容。而是在需要显示内容时修改内容。这样,当用户返回编辑字段时,编辑的是他们输入的内容,而不是您对其内容所做的内容。
使用
wpautop
对文本执行与 WordPress 应用于后内容字段中输入的原始内容相同的翻译,并在请求显示内容时执行此操作。Don't modify the data saved to the database. Save exactly what the user enters. Instead modify the content when you need to display it. This way when the user comes back to edit the field the edit what they put in, not what you've made of their content.
Use
wpautop
to do the same translation on your text that WordPress applies to the raw content entered in the post-content field and do it when the content is requested for display.好吧,我找到了解决我问题的另一种方法。谢谢塔图让我的大脑运转起来。对于那些正在寻找解决方案的人,这就是我所做的:
Okay I have found another solution to my problem. Thank you Tatu for getting my brain workin. For those who are looking for a solution this is what I did: