做网站怎么建站点,服装设计怎么学 从零开始,跨境电商做什么平台好,龙岗注册公司我对这个用于显示JSON API中的自定义字段的the_content过滤器感到绝望.我正在使用此插件http://wordpress.org/plugins/json-rest-api/从自定义帖子类型获得JSON响应.这些自定义帖子类型具有我必须在移动应用程序中显示的自定义字段.为了实现这一点,我编写了以下代码,该代码使用…我对这个用于显示JSON API中的自定义字段的the_content过滤器感到绝望.我正在使用此插件http://wordpress.org/plugins/json-rest-api/从自定义帖子类型获得JSON响应.这些自定义帖子类型具有我必须在移动应用程序中显示的自定义字段.为了实现这一点,我编写了以下代码,该代码使用the_content过滤器替换原始内容,仅使用HTML标签显示自定义帖子类型add_filter( the_content, add_custom_post_fields_to_the_content );function add_custom_post_fields_to_the_content( $content ){global $post;$custom_fields get_post_custom($post-ID); $content ; $content $content.;$content $content..$post-post_title.;$content $content..$custom_fields[wpcf-direccion][0].;$content $content..$custom_fields[wpcf-phone][0].;$content $content..$custom_fields[wpcf-facebook][0].;return $content;}因此,当我通过浏览器请求信息时,这是一个示例http://bride2be.com.mx/ceremonia/,自定义字段显示得很好,但是当我请求JSON数据时,仅显示HTML,而没有自定义字段的值.这是一个例子我对此不知所措,有人可以帮助我吗解决方法:您使用the_content过滤器的方式不仅在JSON API调用中,而且在各处都得到应用.无论如何,您应该尝试将钩子添加到插件,而不是WordPress(至少不是第一次尝试).以下未经测试,但我相信是正确的轨道/* Plugin Name: Modify JSON for CPT */add_action( plugins_loaded, add_filter_so_19646036 );# Load at a safe pointfunction add_filter_so_19646036(){add_filter( json_prepare_post, apply_filter_so_19646036, 10, 3 );}function apply_filter_so_19646036( $_post, $post, $context ){# Just a guessif( my_custom_type $post[post_type] )$_post[content] my json content;# Brute force debug// var_dump( $_post );// var_dump( $post );// var_dump( $context );// die();return $_post;}您必须设置为inspect all three parameters,以确保这会在正确的帖子类型中发生,并且您在正确地操作$_post.标签wordpress-plugin,wordpress,json,php来源 https://codeday.me/bug/20191030/1966534.html