How to showing Post ID on RSS?

This my code:

$rss = new DOMDocument();
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
//          'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        array_push($feed, $item);
    $limit = 5;
    echo '<div>';
    echo '<ul class="soo-latest">';
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
//      $description = $feed[$x]['desc'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        echo '<li>&raquo; <a target="_blank" class="soo-info" href="'.$link.'">'.$title.'</a>';
        echo '<small class="help">Posted on '.$date.'</small></li>';
//      echo '<p>'.$description.'</p>';
    echo '</ul>';
    echo '</div>';

this my permalink:


I want using wp_get_shortlink(get_the_ID()); for:

echo '<li>&raquo; <a target="_blank" class="soo-info" href="'.$link.'">'.$title.'</a>';

So showing shorten not long link...


You can add the ID as custom tag to your feed via hook. But it give no valid tag in the rss/atom standard to store this. So that I think you should split the guid tag to get the ID of a post, like the follow example.

The guid inside the feed:

<guid isPermaLink="false">https://www.ciusan.com/?p=3566</guid>

Split the ID.

$id = explode( '=', $guid );

The var $guid should you enhance in your foreach loop, so that it store each guid to each item. The $id get a array with all strings there a split by the string =, also your post id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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