0

I used this piece of code in functions.php to add featured image in <image> tag in feed

add_action('rss2_item', 'add_my_rss_node');

function add_my_rss_node() {
    global $post;
    if(has_post_thumbnail($post->ID)):
        $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
         $url = $thumbnail[0];
        echo("<image>{$url}</image>");
    endif;
}

then the URL of featured image displayed in feed as I expected.

Now I want to pull this URL out, I tried <pre>$item->get_title()</pre> but it didn't show anything.

Now I wonder how can I get this URL out?

this' my feed: http://techdaily.vn/feed and I follow this guide to use feed_fetch http://codex.wordpress.org/Function_Reference/fetch_feed

1 Answer 1

1

I found alternative solution for my problem. Instead of using fetch_feed, I used DOMDocument in PHP. This is my code

<?php
$rss = new DOMDocument();
$rss->load('http://techdaily.vn/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'image' => $node->getElementsByTagName('image')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 1;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $image = $feed[$x]['image'];
    ?>
<a href="http://techdaily.vn" target="_blank"><img class="logo" src="http://techdaily.s3.amazonaws.com/wp-content/uploads/2014/01/techdaily-logo1.png"></a>
<ul>
    <li class="thumb">
        <a href="<?php echo $link; ?>" class="title" target="_blank">
            <img src="<?php echo $image; ?>" alt="<?php echo $title; ?>">
        </a>
    </li>
    <li><a href="<?php echo $link; ?>" class="title" target="_blank"><?php echo $title; ?></a></li>
</ul>
1
  • DOM semmed not a good solution, I will tried other one
    – PhamThang
    Commented Jan 27, 2014 at 4:59

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.