php之将 nl2br 添加到我的 Wordpress 自定义元框

findumars 阅读:238 2025-06-02 22:19:02 评论:0

好吧,让我看看我能否解释清楚。在 wordpress 中,我们有一个框来插入摘录。我们需要添加第二个摘录框。我没有在每个帖子中手动添加自定义字段,而是放置了一个功能,可以在管理帖子页面上以元框的形式自动添加自定义字段。

好吧,这就是我使用此功能的问题,除了以下事实:无论您在该字段中输入什么,它都会丢失换行符。因此,当我们的作者为了保持文本 block 的格式而对该字段做出贡献时,我必须手动将
添加到段落的末尾。

这是我的代码:

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

谢谢,任何帮助都可以。

请您参考如下方法:

在前端模板上使用 wpautop 函数。喜欢:

<?php $yourvalue =  get_post_meta($post->ID, "yourvalue", true);    
  if ($yourvalue != ""){ ?> 
   <dt>Consult&oacute;rio:</dt> 
   <dd><?php echo wpautop( $consultorio, $br = 1 ); ?></dd> 
<?php } ?> 


标签:PHP
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号