0

I need to do custom RSS file, And i need to show only pictuer from post_content and put it in <img> tag and remove any word, from post_content .

So how can i display only images from post_content .

<?php foreach ($posts as $post) {             
$content = get_the_content();
$postOutput = preg_replace('#<a[^>]+>([\r\n|\n]+)?<img[^>]+>([\r\n|\n]+)?<\/a>#','', $post->post_content);
//$postContent = preg_replace("/<a[^>]+\>/i", "", $postOutput);


?>
<article>
        <id></id>
            <title><?php echo get_the_title($post->ID); ?></title>
            <link><?php echo get_permalink($post->ID); ?></link>
            <abstract></abstract>
            <body><?php echo '<![CDATA['.$postOutput.'<br/><br/>more: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></body>
                <date><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></date>
                <image_original>....</image_original>
                <image_thumb>....</image_thumb>
                <image_original_alt>....</image_original_alt>
                <slideshow_images>
                    <image>
                        <image_url>.....</image_url>
                        <image_thumb_url>.....</image_thumb_url>
                        <image_alt>.....</image_alt>
                    </image>
                    <image>
                        <image_url>.....</image_url>
                        <image_thumb_url>.....</image_thumb_url>
                        <image_alt>.....</image_alt>
                    </image>
                    <image>
                        <image_url>.....</image_url>
                        <image_thumb_url>.....</image_thumb_url>
                        <image_alt>.....</image_alt>
                    </image>
                </slideshow_images>
</article>
<?php } ?>
4
  • Are you asking how to generate an RSS feed that ONLY shows images? Commented Jun 3, 2015 at 16:05
  • yes but not the default rss , i do custom rss file Commented Jun 3, 2015 at 16:12
  • Do you already have the custom RSS file that shows all the content? If so, what does your code look like? Commented Jun 3, 2015 at 16:14
  • see my edit post Commented Jun 3, 2015 at 17:00

1 Answer 1

0

you can use regular expression on your post content.Which could be done for example like this:

for reference

$content = get_the_content();
// we need a expression to match things
$regex = '/src="([^"]*)"/';
// we want all matches
preg_match_all( $regex, $content, $matches );
// reversing the matches array
$matches = array_reverse($matches);
echo '<pre>';
// we've reversed the array, so index 0 returns the result
print_r($matches[0]);
echo '</pre>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.