WP Rest API - So erhalten Sie ein ausgewähltes Bild
6 Antworten
- Stimmen
-
- 2016-06-30
Sehen Sie sichein Pluginmit dem Namen Besseres REST-API-Bild .Die hinzugefügte Bild-URL wird der ursprünglichen API-Antwort hinzugefügt.
Take a look at a plugin called Better REST API Featured Image. It adds the featured image URL to the original API response.
-
Vielen Dank.Gibt die URL zurück,diepraktischist.Irgendwelche Ideen,warum das Plugin selbstesnicht zurückgibt?Macheichetwasfalsch oderistes die API?Thank you. Returns the URL which is handy. Any ideas why the plugin it self is not returning it? Am I doing something wrong or is it the API?
- 0
- 2016-06-30
- Abdul Sadik Yalcin
-
Esist die API.Nochfrühe Tage.Es wird sich verbessernIts the API. Still early days. It will improve
- 1
- 2016-06-30
- Michael Cropper
-
Problemgelöst!Esgibt zwareine ID des Bildes zurück,aberich habe völlig vergessen,dassich den Cache aktiviert habe!Trotzdemist dieses Pluginbesser,daes die URL direkt zurückgibt.Problem solved! It does actually return an ID of the image but I totally forgot I had the cache turned on! But anyway, that plugin is better as it returns the url directly.
- 1
- 2016-06-30
- Abdul Sadik Yalcin
-
@ Devrim Schön,dass duesgelöst hast!Wenn Ihnen diese Antwort von Michaelgeholfen hat,können Sie auf das Häkchen links davon klicken,um sie zu akzeptieren und anderen zu zeigen,dass dies die richtige Antwort war.:) :)@Devrim Glad you solved it! If this answer from Michael helped you, you can click the tick to the left of it to accept it to show others that this was the correct answer. :)
- 0
- 2016-06-30
- Tim Malone
-
- 2017-05-31
Sie könnenes ohne Pluginserhalten,indem Sie Ihrer Abfrage
_embed
als Parameter hinzufügen/?rest_route=/wp/v2/posts&_embed /wp-json/wp/v2/posts?_embed
You can get it without plugins by adding
_embed
as param to your query/?rest_route=/wp/v2/posts&_embed /wp-json/wp/v2/posts?_embed
-
Dies verursachtein Problembeim Bindenin einer Winkelbindung. Führen Sie den Namen "wp:"eines der Knotenim JSON-Pfad zum Bild aus.Ich habe das Plugin aus der anderen Antwort verwendet,was den Pfad zum Bild vereinfacht.this causes a problem when binding to it in an Angular binding, do to the `wp:` name of one of the nodes in the json path to the image. I used the plugin from the other answer, which simplifies the path to the image.
- 0
- 2017-10-09
- Steve
-
Nachteile: Der JSON wird schwerer Profis: Pluginnichtinstallieren,keine andere http-Anfrage aufrufen -> upvotecons: the JSON get heavier pros: not install plugin, not call another http request -> upvote
- 1
- 2017-11-21
- Tho Vo
-
Wie soll wp:featuremediain JSON konvertiert werden?Zuersterstelleicheine WP-Klasse,die vorgestellte Medienenthält.Aberesfunktioniertnicht.How should convert wp:featuredmedia to JSON? first I create wp class that contains featuredmedia. But it does not work.
- 1
- 2018-04-03
- Mahdi
-
Sie können auf wp: zugreifen,indem Sie diese Notation verwenden. Post._embedded ['wp:term']You can access wp: by using this notation post._embedded['wp:term']
- 3
- 2018-05-15
- ocajian
-
- 2018-10-22
Ich würde NICHT dasbessere Rest-API-Plugin verwenden.Es hat der restlichen API vorgestellte Bilder hinzugefügt,aberes hates auch kaputtgemacht.
Diesist dieeinfachste Lösung,dieichgefunden habe und dietatsächlichfunktioniert hat.Fügen Sie Ihrerfunctions.php denfolgenden Code hinzu:
<?php function post_featured_image_json( $data, $post, $context ) { $featured_image_id = $data->data['featured_media']; // get featured image id $featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' ); // get url of the original size if( $featured_image_url ) { $data->data['featured_image_url'] = $featured_image_url[0]; } return $data; } add_filter( 'rest_prepare_post', 'post_featured_image_json', 10, 3 );
I would NOT use the better rest API plugin. It did add featured images to the rest api but it also broke it.
This is the simplest solution I was able to find that actually worked. Add the following code to your functions.php:
<?php function post_featured_image_json( $data, $post, $context ) { $featured_image_id = $data->data['featured_media']; // get featured image id $featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' ); // get url of the original size if( $featured_image_url ) { $data->data['featured_image_url'] = $featured_image_url[0]; } return $data; } add_filter( 'rest_prepare_post', 'post_featured_image_json', 10, 3 );
-
-
- 2018-09-28
Ich habeeine Verknüpfung zumeinem Bilderstellt,indemich sie direkt zur API-Antwort hinzugefügt habe.
//Add in functions.php, this hook is for my 'regions' post type add_action( 'rest_api_init', 'create_api_posts_meta_field' ); function create_api_posts_meta_field() { register_rest_field( 'regions', 'group', array( 'get_callback' => 'get_post_meta_for_api', 'schema' => null, ) ); }
//Use the post ID to query the image and add it to your payload function get_post_meta_for_api( $object ) { $post_id = $object['id']; $post_meta = get_post_meta( $post_id ); $post_image = get_post_thumbnail_id( $post_id ); $post_meta["group_image"] = wp_get_attachment_image_src($post_image)[0]; return $post_meta; }
I made a shortcut to my image by adding it directly to the API response.
//Add in functions.php, this hook is for my 'regions' post type add_action( 'rest_api_init', 'create_api_posts_meta_field' ); function create_api_posts_meta_field() { register_rest_field( 'regions', 'group', array( 'get_callback' => 'get_post_meta_for_api', 'schema' => null, ) ); }
//Use the post ID to query the image and add it to your payload function get_post_meta_for_api( $object ) { $post_id = $object['id']; $post_meta = get_post_meta( $post_id ); $post_image = get_post_thumbnail_id( $post_id ); $post_meta["group_image"] = wp_get_attachment_image_src($post_image)[0]; return $post_meta; }
-
- 2020-07-15
Versuchen Siees auffolgende Weise ....................
URL:
/wp-json/wp/v2/posts?_embed
image:
json["_embedded" weibl. ["
Esfunktioniertgut. Versuch
Try following way ....................
URL:
/wp-json/wp/v2/posts?_embed
image:
json["_embedded"]["wp:featuredmedia"][0]["source_url"],
It's working fine.try
Ichbin sehrneuin dieser API,tatsächlich habeichbishernurein paar Stunden damit verbracht. Ich habemeine Nachforschungen angestellt,kann abernichts darüberfinden ...
Das Problemist,dassich anscheinendnicht das Bildeines Beitragserhalten kann. Der JSONgibt
"featured_media: 0"
zurück.Ich habe definitivein Bildfür den Beitragfestgelegt,aber die Datengeben Folgendes zurück:
Jede Hilfe wirdgeschätzt.