Чтобы получить содержимое публикации или отрывка на разных языках программирования, вы можете использовать разные методы. Вот примеры на нескольких популярных языках:
-
Python (WordPress):
import requests def get_post_content(post_id): url = f"https://example.com/wp-json/wp/v2/posts/{post_id}" response = requests.get(url) data = response.json() content = data['content']['rendered'] return content post_id = 123 # Replace with the actual post ID post_content = get_post_content(post_id) print(post_content) -
PHP (WordPress):
function get_post_content($post_id) { $url = "https://example.com/wp-json/wp/v2/posts/{$post_id}"; $response = wp_remote_get($url); $data = json_decode(wp_remote_retrieve_body($response), true); $content = $data['content']['rendered']; return $content; } $post_id = 123; // Replace with the actual post ID $post_content = get_post_content($post_id); echo $post_content; -
JavaScript (WordPress REST API):
function getPostContent(postId) { fetch(`https://example.com/wp-json/wp/v2/posts/${postId}`) .then(response => response.json()) .then(data => { const content = data.content.rendered; console.log(content); }); } const postId = 123; // Replace with the actual post ID getPostContent(postId); -
Ruby (WordPress REST API):
require 'net/http' require 'json' def get_post_content(post_id) url = URI.parse("https://example.com/wp-json/wp/v2/posts/#{post_id}") response = Net::HTTP.get_response(url) data = JSON.parse(response.body) content = data['content']['rendered'] return content end post_id = 123 # Replace with the actual post ID post_content = get_post_content(post_id) puts post_content -
Java (WordPress REST API с использованием OkHttp):
import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.json.JSONObject; public class Main { public static void main(String[] args) throws Exception { OkHttpClient client = new OkHttpClient(); int postId = 123; // Replace with the actual post ID String url = "https://example.com/wp-json/wp/v2/posts/" + postId; Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); JSONObject jsonObject = new JSONObject(response.body().string()); String content = jsonObject.getJSONObject("content").getString("rendered"); System.out.println(content); } }
Эти примеры демонстрируют, как получить содержимое сообщения или отрывка с помощью разных языков программирования. Обязательно замените « https://example.com » фактическим URL-адресом вашего сайта WordPress.