Jimbox
Jimbox

Reputation: 11

<image> inside a sitemap.xml PHP script?

I'm working on a sitemap plugin for WordPress. You may think why, as there are lot of great sitemap plugins. Well, I wrote one because I needed a sitemap which works for specific categories. I did it... very simple for now, but I'm working daily to improve it.

Right now sitemap take url, lastmod, changefreq and priority, and my part of the script look like this :

foreach($postsForSitemap as $post) { setup_postdata($post);

$postdate = explode(" ", $post->post_modified);

$sitemap .= '<url>'.
  '<loc>'. get_permalink($post->ID) .'</loc>'.
  '<lastmod>'. $postdate[0] .'</lastmod>'.
  '<changefreq>daily</changefreq>'.
  '<priority>0.8</priority>'.
'</url>';

How I can add the images included on the posts? just to add also:

    <image:image>
        <image:loc>imageurl-1.jpg</image:loc>
        <image:title><![CDATA[image title]]></image:title>
        <image:caption><![CDATA[image alt]]></image:caption>
    </image:image>

    <image:image>
        <image:loc>imageurl-2.jpg</image:loc>
        <image:title><![CDATA[image title]]></image:title>
        <image:caption><![CDATA[image alt]]></image:caption>
    </image:image>

ecc ...

Also, how can I add a function that remove an url if I delete a post from the wp-admin?

Upvotes: 1

Views: 400

Answers (1)

Delonix
Delonix

Reputation: 31

I don't know if I understood your question. In your example, the only observation I would make would be regarding the value of image: loc. I think you should enter the complete path to the image.

<url>
    <loc>https://post_url</loc>
    <image:image>
        <image:loc>https://image_url</image:loc>
        <image:title><![CDATA[image title]]></image:title>
        <image:caption><![CDATA[image alt]]></image:caption>
    </image:image>
</url>  

Upvotes: 1

Related Questions