Domanda

I'm fetching an RSS feed with Simplepie on a Wordpress blog. Each RSS item has a div container, and I want each one to have a unique id number (something like <div id="div-#">).

How would I generate a unique number for each item? Here's the code I'm using:

<ul>

<?php include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed(http://www.example.com);
if (!is_wp_error( $rss ) ) :  
$maxitems = $rss->get_item_quantity(6); 
    $rss_items = $rss->get_items(0, $maxitems); 
    endif; ?>

<?php 
    if ($maxitems == 0) echo '<li>No items.</li>';
    foreach ( $rss_items as $item ) :  ?>

        <a href="<?php   echo $item->get_link();?>">


        <?php  
                echo '<li><div class="item-headline" id="div-">'.
                $item->get_title().
                '</div><div class="item-info">'.
                $item->get_description().
                '</div></li>';
            ?>

        </a>    

<?php endforeach; ?>

    </ul>
È stato utile?

Soluzione

Use get_id(true) to get a unique MD5 hash of the item.

echo '<li><div class="item-headline" id="div-'.$item->get_id(true).'">'.

Altri suggerimenti

Try something like:

<?php
foreach ($feed->get_items() as $item) {
    echo '<div id="item-<?php echo $num ?>">';
    // ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top