abrudtkuhl / WordPressSharp

A C# client to to interact with the WordPress XML-RPC API
abrudtkuhl.github.io/WordPressSharp/
GNU General Public License v3.0
103 stars 49 forks source link

How to get all the photo attachments in an article #77

Open Blackcat156 opened 6 years ago

Blackcat156 commented 6 years ago

I want to get all the photo attachments in a published post. https://codex.wordpress.org/XML-RPC_WordPress_API The wp.uploadFile provided by WordPress can't be found in the API How can I get a ID to get all the pictures in the published article post.MediaItems It's always null I want to get the picture ID, thank you!

Blackcat156 commented 6 years ago

var client = new WordPressClient(StaticValue.LocalUser); Post GetPost = client.GetPost(Convert.ToInt32(text)); Console.WriteLine(GetPost.Content);

        Console.WriteLine(GetPost.MediaItems==null);
       //Output true

default I have uploaded the picture

Blackcat156 commented 6 years ago

Hello, I can't solve the problem. So I use WordPress to output all the ID in the picture and then delete them. I hope to help people who have the same problems with me. You need to put this code anywhere in your WordPress theme output article. He will output all the attachments that are uploaded to this article, ID.

`<?php /**

function echo_first_image ($postID) {
$args = array(

'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment'
);

$attachments = get_children( $args );

if ($attachments) {
    foreach($attachments as $attachment) {

        echo '<div class="imgId">'.( $attachment->ID ).'</div> ';

    }
}

} echo_first_image ($postID);

?>`

Blackcat156 commented 6 years ago

https://www.xxxxxx.com/wp-json/wp/v2/media/21370 I found that the appendix ID can be obtained through wp-json, which is automatically opened