17.3. How to build RSS blocks with variable number of news items

If you have created an RSS block from the block administration fuction of the administration panel, you may have noticed that, notably after some change on a previously working RSS block (for example, in the title, or the refresh time), or even from the very start, you get the error:

Currently there is a problem with headlines from this site

This is not always due to some error on your part - it is a bug too. Often, the only solution would be to delete the block and add it anew.

The problem is in the file admin/modules/blocks.php. There, the problematic code is

for ($i=0;$i<10;$i++) {
    $link = ereg_replace(".*<link>","",$items[$i]);
    $link = ereg_replace("</link>.*","",$link);
    $title2 = ereg_replace(".*<title>","",$items[$i]);
    $title2 = ereg_replace("</title>.*","",$title2);
    if ($items[$i] == "" AND $cont != 1) {
        $content = "";
    } else {    
        if (strcmp($link,$title2) AND $items[$i] != "") {
            $cont = 1;  
            $content .= "<strong><big>&middot;</big></strong>&nbsp;<a href=\"$link\" target=\"new\">$title2</a><br>\n";
        }           
    }           
}           

which has the number of news items (10) hardcoded into it. The problem is caused by the fact that, if there aren't that many headlines being returned, and in many unlucky cases, there aren't, you will either get an array out of bounds error, or nothing will be returned, and the content of the block will be set to equal nothing.

You could fix that by changing the “10” in the code above to “5”, for example. However, that fix wouldn't work for a site that had only three headlines in their RSS file. What needs to happen is to modify the

        $content = "";
to
        $content += "";

For a modification of the mainfile.php and admin/modules/blocks.php files that also allows for a variable number of items to be chosen from the administration panel individually for each RSS block, see Digital Nick Downloads, for a discussion thread on this bug, see Problems when saving RSS block.