gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:19:59 25 4
gpt4 key购买 nike

好吧,让我看看我能否解释清楚。在 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 - 将 nl2br 添加到我的 Wordpress 自定义元框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2007528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com