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_met开发者_如何转开发a_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.
Use wpautop function on frontend template. Like:
<?php $yourvalue = get_post_meta($post->ID, "yourvalue", true);
if ($yourvalue != ""){ ?>
<dt>Consultório:</dt>
<dd><?php echo wpautop( $consultorio, $br = 1 ); ?></dd>
<?php } ?>
Just add this line after $new_meta_value = ...
:
$new_meta_value = nl2br($new_meta_value);
And instead of comparing your values to ''
, it's better to use empty()
. Also some of the comparisons are unneeded. Thus, the add/update/delete part of your save function can be written like this:
if(empty($meta_value)) {
add_post_meta( $post_id, 'Second Excerpt', $new_meta_value, true );
} elseif(empty($new_meta_value)) {
delete_post_meta( $post_id, 'Second Excerpt', $meta_value );
} else {
update_post_meta( $post_id, 'Second Excerpt', $new_meta_value );
}
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.
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:
$new_meta_value = "<p>" . implode( "</p>\n\n<p>", preg_split( '/\n(?:\s*\n)+/', $new_meta_value ) ) . "</p>";
精彩评论