I am confused with a simple issue, Actually, I am using Wordpress Custom fields using post meta
echo get_post_meta( $post->ID, '_text_field', true );
Above meta code generate a value entered in a custom field.
But, I want to add html code around it.
If i add paragraph to it like this <p> <?php echo get_post_meta( $post->ID, '_text_field', true ); ?> </p> then it is fine! but when field is empty then it shows a empty <p></p>
That's why i am looking to add a html in meta code so that if field is empty then no html code will be included in DOM.
And if field have text then it shows text with a html markup.
i am not sure how to do this!
Any idea?
Use the wpautop() function to automatically add paragraphs instead.
Example:
echo wpautop( get_post_meta( $post->ID, '_text_field', true ) );
You should probably be assigning this meta field to a variable first and checking the value is set before you attempt to output.
E.g.
$text_field = get_post_meta( $post->ID, '_text_field', true );
if ( ! empty( $text_field ) ) {
echo wpautop( $text_field );
}
Related
I set the php code for meta description in normal entrys in WP, and I have set an additional php code for AMP template to replicate the same text in the meta description tag but I donĂ½ know how to vinculate them.
this is php code for generic meta description:
function wpse_custom_meta_description(){
if ( ! is_single() && ! is_page())
return;
$desc = get_post_meta( get_queried_object_id(), 'description', true );
if( ! empty( $desc ) )
printf(
'<meta name="description" content="%s" />',
esc_attr( trim( $desc ) )
);
}
add_action( 'wp_head', 'wpse_custom_meta_description' , 2 );
...and this is the code for meta description in AMP, but I would like that both formats had the same text, how can I vinculate AMP text to generic?
add_action('amp_post_template_head', function() {
echo '<meta name="description" content="'wpse_custom_meta_description'">';
});
I mean, in the AMP php code I need to vinculate to "wpse_custom_meta_description" function.
Thank you!
I have a custom page template with certain custom fields. I want to display these custom fields outside the loop, but within the same page.
This one works:
<?php echo get_post_meta( '244', 'custom_field_name', true ) ?>
But I want to to work dynamically, without me entering the actual ID of the page.
How can I call the page ID in the echo?
Try this :
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>
If this call is in the loop, replace the id with function get_the_ID, this function retrieves the ID of the current item in the WordPress Loop.
<?php echo get_post_meta( get_the_ID(), 'custom_field_name', true ) ?>
See: https://developer.wordpress.org/reference/functions/get_the_ID/
If this call is in the single page, replace the id with object item $post->ID.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Class_Reference/WP_Post
Also, you can get the access to the post via global variable $post.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Global_Variables
I have a custom code to update a custom field from the front end. It loads the data correctly but when I try to change/update the custom field it goes wrong. It updates the meta value, but it has more values.
These are the meta values:
sp_metrics'a:3:{s:15:"ledennummerknvb";s:5:"12659";s:6:"height";s:1:"5";s:6:"weight";s:1:"5";}'
When I try to update "ledennummerknvb" it goes terribly wrong.
Here's the code I use to create the custom field:
function your_function_name( $form_id, $post_id, $form_settings ) {
$value = '';
if ( $post_id ) {
$metrics = get_post_meta( $post_id, 'sp_metrics', true );
$ledennummerknvb = $metrics['ledennummerknvb'];
}
?>
<div class="wpuf-label">
<label>Ledennummer KNVB</label>
</div>
<div class="wpuf-fields">
<input type="text" name="my_custom_field" value="<?php echo( $ledennummerknvb ); ?>">
</div>
<?php
}
add_action( 'my_brand_new_hook', 'your_function_name', 10, 3 );
function update_my_brand_new_hook( $post_id ) {
if ( isset( $_POST['my_custom_field'] ) ) {
update_post_meta( $post_id, 'sp_metrics', $_POST['my_custom_field'] );
}
}
add_action( 'wpuf_add_post_after_insert', 'update_my_brand_new_hook' );
add_action( 'wpuf_edit_post_after_update', 'update_my_brand_new_hook' );
So when I update the custom field it overwrites every value plus it will give me back a result of just 1 number. For instance now you see at "ledennummerknvb" the numbers are "12659" but when I change or update them it goes wrong.
I hope someone can help and or explain me what I'm doing wrong as I don't have the knowledge to figure it out.
I have install plugin Yoast SEO Premium,
with this plugin have some field is
yoast_wpseo_metadesc,yoast_wpseo_focuskw
but I can't add althought I used:
add_post_meta($post_id, 'yoast_wpseo_metadesc',$my_post['post_excerpt']);
This problem may be due to several concerns, here are some ideas :
Prefix meta keys with "_"
Yoast SEO prefix these custom fields entries with a "_" in the database. The key is not "yoast_wpseo_metadesc" but "_yoast_wpseo_metadesc". Same for "yoast_wpseo_focuskw", it's actually "_yoast_wpseo_focuskw".
Use update_post_meta() instead of add_post_meta()
Add_post_meta() can create a custom field for a post if the field does not exist. If it exists, it does not update it. It's always better to use the update_post_meta() function, more flexible. If the custom field already exists, it will be updated. Otherwise, the function will call add_post_meta() to create it.
In your case, a field maybe already exists with that name.
Be careful to where you execute your code
I think we need more information on where you launch your add_post_meta() function. Depending on the context, the approach is different. Here are some examples:
In single post page
If it's on your single page, you can use :
<?php
global $post;
update_post_meta( $post->ID, '_yoast_wpseo_metadesc', $post->post_excerpt );
update_post_meta( $post->ID, '_yoast_wpseo_focuskw', my_focus_keyword' );
?>
In function.php
Place this code in functions.php, it do that after each post saving, in admin section.
<?php
// Launch the update_post_meta on post saving.
add_action( 'save_post', 'my_yoast_saved_datas' );
function my_yoast_saved_datas( $post_id, $post ) {
// Check that your post is what you want
if ( $_POST['post_type'] == 'post' ) {
// Check if user can't do that
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
}
update_post_meta( $post_id, '_yoast_wpseo_metadesc', $post->post_excerpt );
update_post_meta( $post_id, '_yoast_wpseo_focuskw', 'my_focus_keyword');
}
?>
Loop to update custom fields in one time
Launch one time, in functions.php or pack it on a plugin to launch on activation.
<?php
global $post;
// Array of args, change to your need
$args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'post_status'=> 'publish' );
// Get alls posts
$my_posts = get_posts( $args );
// Loop on post
foreach ( $myposts as $post ) {
setup_postdata( $post );
global $post;
// Update on create custom fields
update_post_meta( $post->ID, '_yoast_wpseo_metadesc', $post->post_excerpt );
update_post_meta( $post->ID, '_yoast_wpseo_focuskw', 'my_focus_keyword');
}
wp_reset_postdata();
?>
I'm sending array meta box data using update_post_meta like below. However I cannot seem to output the post meta array value into an empty input. The meta is being stored correctly.
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}
My input field looks like this:
<input type="date" class="widefat" name="vp-date" id="vp-date" value="<?php echo $date_info['vp-date']; ?>" />
I also get a notice which traces back to the if get_post_meta function above. It says:
Trying to get property of non-object in
Any help would be great.
Thanks
That error message means that the $post is $post->ID is not an object. I don't know what script you are in but try putting
global $post;
above the if.
Making it
global $post;
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}