I am trying to:
output custom field (wpcf) with json
I visited this URL:
I expected to see: output Valueof filed
Instead, I got: none ,empty filed
Hello,
Thank you for your message!
If I understand correctly, you have an array stored in a custom field and you want to convert it in a JSON format, that's right?
You can try this:
<?php
$field_in_json_format = json_encode( types_render_field( 'my-custom-field', array( 'output' => 'raw' ) ) );
?>
Check the documentation for json_encode() function: hidden link
Let me know if this helps.
Regards,
Pierre
I do not understand how to use it....
i have a custom post type template with a custom field....
<span class="citizen"><?php echo types_render_field("citizen", array(""));?></span>
And I want to output this file in json ( i mark "herer" where i want to disply this filed:
---------------------------------------------------------------------------
function showPost(id) {
$.getJSON('hidden link' + id + '&callback=?', function(data) {
var output = '<h3>' + data.post.title + '</h3>';
output += data.post.content;
$('#mypost').html(output);
});
$.getJSON('hidden link' + id + '&callback=?', function(data) {
var output = '<h3>' + data.post.title + '</h3>';
output += data.post.content;
output += $field_in_json_format; <<<<<<<---------- here?????
$('#mypost').html(output);
});
}
As far as I can see, you are making an Ajax request to get a post (javascript executed in the brower).
I don't know the exact structure of your code but I assume the function returning the post data should include the custom field data like this:
// JavaScript
// ...
output += data.post.my_custom_field;
// ...
To add this custom field to the "data" object, it depends on the code structure, but it should be something like this:
<?php
// ... Create data object
// ... Add the post object to the data object
// Add the custom field to the post object
$data->post->my_custom_field = types_render_field( 'my-custom-field', array( 'output' => 'raw' ) );
echo json_encode( $data );
die();
Could share an extract of your server-side code to return the data from the Ajax call?
Regards,
Pierre
its output "undefined"....
Can you share an extract of the code called by "hidden link' + id + '&callback=?" ?
Regards,
Pierre
How do I write to you privately?
Your next reply will be private.
Regards,
Pierre
You get my private
message?
Hello,
I managed to see your private reply but this doesn't help.
To help you on your project, I need to have a look at the PHP code handling the ajax request.
Are you using a plugin to handle this ajax request? Can you share the PHP file?
Regards,
Pierre
i can send this Details privately?
Your next reply will be private.
Regards,
Pierre
Hello,
I had a look at the plugin JSON API, and as far as I can see in the documentation https://wordpress.org/plugins/json-api/other_notes/#3.2.-Content-modifying-arguments, you have to specify which custom field you need in the arguments of your request.
Your request URL should be like this:
<em><u>hidden link</u></em>?
Does it makes sense to you?
Regards,
Pierre
I still need help...It does not work