Skip to content
Menu
HE'S BLOG
  • Home
  • One Page
  • Home page
  • Links
  • Mac
  • Windows
  • Program
  • AI
HE'S BLOG
2020年3月18日2020年5月2日

Rest FUll API 中添加WTI LIKE POST 插件的喜欢数。

WordPress 的 WTI LIKE POST 插件的喜欢数默认是添加在Content中的,而在Wordpress 的REST FULL API 中没有喜欢数相关字段,只需在wti_like_post.php中添加以下代码就能够在RESTFULL API中添加likes字段了:

add_action( 'rest_api_init', function () {
register_rest_field( 'post', 'likes', array(
'get_callback' => function( $post_arr ) {
return GetWtiLikeCount($post_arr['id']);
},
'schema' => array(
'description' => __( 'Likes .' ),
'type' => 'integer'
),
) );
} );

当API请求时,文章内容中不显示点赞按钮, 在wti_like_post_site.php中的函数function PutWtiLikePost($content) 添加以下代码,

if( isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'],'/wp-json/wp/v2/')!== false ){
$show_on_pages = false;
}

参考:https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#how-register_rest_field-works

近期文章

  • Dify1.3.1安装
  • Gopro 驱动下载
  • Popular models download website
  • Stable diffusion enable remote access
  • Ollama remote access

近期评论

    归档

    • 2025 年 5 月
    • 2025 年 3 月
    • 2025 年 2 月
    • 2024 年 3 月
    • 2023 年 10 月
    • 2023 年 5 月
    • 2023 年 4 月
    • 2022 年 11 月
    • 2022 年 9 月
    • 2022 年 5 月
    • 2022 年 4 月
    • 2022 年 3 月
    • 2022 年 2 月
    • 2022 年 1 月
    • 2021 年 10 月
    • 2021 年 9 月
    • 2021 年 5 月
    • 2020 年 7 月
    • 2020 年 6 月
    • 2020 年 5 月
    • 2020 年 4 月
    • 2020 年 3 月
    • 2019 年 11 月
    • 2019 年 10 月
    • 2018 年 9 月
    • 2018 年 6 月
    • 2018 年 5 月
    • 2018 年 2 月
    • 2017 年 8 月
    • 2016 年 4 月

    分类

    • AI
    • Android
    • Angular
    • Apple Hardware
    • C++
    • Docker
    • Doctine
    • ELK
    • English
    • Hadoop
    • Hbase
    • IDE&Tools
    • Ionic2
    • Javascript
    • jQuery
    • Kubernetes
    • Mac
    • PHP
    • Program
    • Scala
    • Shell
    • SQL
    • Symfony
    • Web
    • Wordpress
    • 一行命令

    其他操作

    • 登录
    • 条目 feed
    • 评论 feed
    • WordPress.org
    ©2025 HE'S BLOG | Powered by WordPress and Superb Themes!