Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Modifying XML image Sitemap, part 2 WordPress

  • REFUNDED

Hello,

Currently I'm using below XML image Sitemap on some of my websites. I've already had some changes made in the past http://wpquestions.com/question/show/id/2549, but I'd like to include yet another one.

At this moment the code is grabbing all images included within the post content.
The adjustment I'd like to have made is to include also the featured images attached to that post.

<?php
/*
Plugin Name: Image XML Sitemap
*/

function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );
if ( $image_url ) {
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {
$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}
add_action( 'template_redirect', 'google_sitemap_image' );

Answers (2)

2012-01-01

Lee Willis answers:

Have you looked at Yoast's WordPress SEO plugin - I believe the XML Sitemaps functionality in that includes images in the XML - might be worth a look:

http://wordpress.org/extend/plugins/wordpress-seo


cor comments:

Hi Lee,

I have. I've seen all actually. And for the tiny bit I understand php his Sitemap is one of the most advanced available.

His plugin however is also includes far too many options I currently require (and are required from me). Therefore I'm using the sitemap + some extras above.

2012-01-01

Romel Apuya answers:

try this.


<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );
$photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
$size='medium';
if ( $image_url ) {
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {

$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($photos) {
$photo = array_shift($photos);
$xml .= "\t\t<image:fet>" . wp_get_attachment_image($photo->ID, $size) . "</image:fet>\n";
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}

add_action( 'template_redirect', 'google_sitemap_image' );


and this one is the good reference.

[[LINK href="http://johnford.is/programmatically-pull-attachments-from-wordpress-posts/"]]http://johnford.is/programmatically-pull-attachments-from-wordpress-posts/
[[/LINK]]


im not that sure about the xml i wrote.
im not so familiar with xml.. :(


cor comments:

Hi Romel,

I've been trying to adjust your code, but I don't think it's exactly what I'm after.

Probably wp_get_attachment_image_src instead of wp_get_attachment_image is required to get what I'm looking for (just the image url)

Besides that I'd like to keep the same functionality as in the original snippet (the url of the post it's attached to)


Romel Apuya comments:

here try this. with the wp_get_attachment_image_src();


<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$image_attributes = wp_get_attachment_image_src( $post->ID );

if ( $image_url ) {
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {

$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($image_attributes) {
$photo = array_shift($photos);
$xml .= "\t\t<image:fet>" . $image_attributes[0] . "</image:fet>\n";;
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}

add_action( 'template_redirect', 'google_sitemap_image' );


Romel Apuya comments:

<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$image_attributes = wp_get_attachment_image_src( $post->ID );

if ( $image_url ) {
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {

$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($image_attributes) {
$xml .= "\t\t<image:fet>" . $image_attributes[0] . "</image:fet>\n";;
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}

add_action( 'template_redirect', 'google_sitemap_image' );


cor comments:

Hi Romel,

I've tried your last snippet, but without any luck.

I've just set up a small demo site http://klanten.24design.net/html5/
The first post contains an image. The second a featured image.
And here's the sitemap to show the output http://klanten.24design.net/html5/sitemap-image.xml


Romel Apuya comments:

ok.. have you tried using

the_post_thumbnail()?

or try this one:


<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
$thumb = wp_get_attachment_image_src($thumb[0], false);
$thumb = $thumb[0];

if ( $image_url ) {
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {
$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($image_attributes) {
$xml .= "\t\t<image:fet>" . $thumb . "</image:fet>\n";;
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}
add_action( 'template_redirect', 'google_sitemap_image' );


Romel Apuya comments:

sorry that was wrong i guess


here:


<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
$thumb = wp_get_attachment_image_src($thumb[0], false);
$thumb = $thumb[0];

if ( $image_url ) {
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {
$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($thumb) {
$xml .= "\t\t<image:fet>" . $thumb . "</image:fet>\n";;
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}
add_action( 'template_redirect', 'google_sitemap_image' );


Romel Apuya comments:

btw. did you tried adding featured image to the first post?

i think that code you had with little alteration of mine
will show the feature image if both post had the image and
a featured image.


Romel Apuya comments:

<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
$thumb = wp_get_attachment_image_src($thumb[0], false);
$thumb = $thumb[0];

$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
if ( $image_url ) {

$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {
$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($thumb) {
$xml .= "\t\t<image:fet>" . $thumb . "</image:fet>\n";
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}
add_action( 'template_redirect', 'google_sitemap_image' );


cor comments:

I'm afraid all are giving me a Undefined offset: 0 notice.

Besides that image:fet is really an alternative for image:loc. It isn't a property of (featured image and/or 'normal' image)
Both having an image url and an url of the post it's attached to.


Romel Apuya comments:

can you give me access to your site?


Romel Apuya comments:

<?php

/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT ID, post_title, post_excerpt, post_content, post_parent
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$thumb = get_post_thumbnail_id($post->ID);
$thumb = wp_get_attachment_image_src($thumb,'thumbnail');
$thumb = $thumb[0];


$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
if ( $image_url ) {
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {
$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($thumb) {
$xml .= "\t\t<image:loc>" . $thumb . "</image:loc>\n";
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}
add_action( 'template_redirect', 'google_sitemap_image' );


cor comments:

I can of course, but I won't :)

If it was a site specific thing I was asking, I of course would.

The snippet from the first post is the whole plugin.


Romel Apuya comments:

ok i reviewed the code.

this one should work.

if not i can take a look at your code physically

cheers,



<?php
/*
Plugin Name: Image XML Sitemap
*/
function google_sitemap_image() {
if ( ! preg_match( "/sitemap\-image\.xml$/", $_SERVER['REQUEST_URI'] ) ) {
return;
}
global $wpdb;
$posts = $wpdb->get_results( "SELECT *
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_password = ''
AND ( post_type = 'post' OR post_type = 'page' OR post_type = 'post_type' )
ORDER BY post_type DESC, post_modified DESC
LIMIT 1000" );
header( 'X-Robots-Tag: noindex, follow', true );
header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";
$xml = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ( $posts as $post ) {
preg_match( '/<img [^>]*src=[\'"]([^\'"]+)[\'"][^>]*>/', $post->post_content, $img );
if ( ! array_key_exists( 1, $img ) )
continue;
$image_url = trim( $img[1] );

$thumb = get_post_thumbnail_id($post->ID);
$thumb = wp_get_attachment_image_src($thumb,'thumbnail');
$thumb = $thumb[0];
$xml .= "<url>\n";
$xml .= "\t<loc>" . get_permalink( $post->ID ) . "</loc>\n";
$xml .= "\t<image:image>\n";
if ( $image_url ) {
$xml .= "\t\t<image:loc>" . $img[1] . "</image:loc>\n";
if ( $post->post_title ) {
$xml .= "\t\t<image:title>" . htmlentities( $post->post_title ) . "</image:title>\n";
}
if ( $post->post_excerpt ) {
$xml .= "\t\t<image:caption>" . htmlentities( $post->post_excerpt ) . "</image:caption>\n";
}
if ($thumb) {
$xml .= "\t\t<image:loc>" . $thumb . "</image:loc>\n";
}
$xml .= "\t</image:image>\n";
$xml .= '</url>' . "\n";
}
}
$xml .= '</urlset>';
echo ( "$xml" );
exit();
}
add_action( 'template_redirect', 'google_sitemap_image' );