Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using fetch_feed (based on Simplepie) to display an array of RSS feeds (favicon & headlines only). Documentation on the simplepie website says to use get_favicon in the stylesheet. And I'm using the fetch_feed codex example. I'm getting the spacing where the favicon should be but not the favicon. The feed links appear fine.

CSS:

.rss a {
padding:0 0 0 20px;
background:transparent url(<?php echo esc_url ( $item->get_favicon() ); ?>) no-repeat 0 1px;
}

PHP:

<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2>

    <?php // Get RSS Feed(s)

        // include_once(ABSPATH . WPINC . '/feed.php');

        // Get a SimplePie feed object from the specified feed source.

        $rss = fetch_feed(array('http://wordpress.org/news/feed/','http://beernews.org/feed/'));

            if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 

                  // Figure out how many total items there are, but limit it to 5. 

                  $maxitems = $rss->get_item_quantity(20); 

                    // Build an array of all the items, starting with element 0 (first element).

                    $rss_items = $rss->get_items(0, $maxitems); 

            endif;

    ?>

<ul style="list-style-type: none;">

    <?php if ($maxitems == 0) echo '<li>No items.</li>';

    else

        // Loop through each feed item and display each item as a hyperlink.

        foreach ( $rss_items as $item ) : ?>

            <li>   

                <a class='rss' href='<?php echo esc_url( $item->get_permalink() ); ?>' title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>

                                      <?php echo esc_html( $item->get_title() ); ?>

                                </a>

                <?php echo '<hr/>'; ?>

            </li>

        <?php endforeach; ?>

</ul>
share|improve this question
    
Please put some effort in your code presentation. Is it really necessary for the left third of the php to be blank? –  Johannes Pille Nov 14 '11 at 3:58
    
I copy-pasted from an excerpt of a larger file. Hell of a lot faster than me deleting all the spaces and re-indenting everything. Just scroll over & your 1/3 issue goes away. Cheers! –  Adam Nov 14 '11 at 5:08
    
Properly formatted code yields several advantages: You are more likely to receive a satisfactory response if your code is legible. Also, it will aid others with similar problems reading your question later on. This site has a mere 27 users that can edit w/o peer-review and 6 that can review if others edit. You however, could fix the code yourself without requiring others to spend the extra time. I gladly edit for users that don't know how to format. I am reluctant to do it here and bother moderators that already have a load of other tasks. It would be a matter of courtesy, if you did. Cheers! –  Johannes Pille Nov 14 '11 at 5:34
    
Took nearly 6 minutes but I did it. Hope it helps! –  Adam Nov 14 '11 at 13:44
add comment

3 Answers

You can't ( and shouldn't ) have php in your css files, css should be static and unchanging. If you need to generate dynamic css, you can put it inline in the header, or have your theme add a url via wordpress from which to generate the css stylesheet dynamically.

add_action('wp_head',createfunction("","echo '<style>.rss a { background-image: url(".esc_url ( $item->get_favicon() )."); }</style>';"));

If you made the CSS PHP executable, the browser could ( and probably will ) cache the stylesheet making the icon incorrect on some pages when using multiple feeds. It would also be a security issue.

share|improve this answer
add comment

You have to tell apache to parse css files for php. One way is to edit .htaccess and add the directive below to get php to parse css files.

AddType application/x-httpd-php .css

See http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/

share|improve this answer
    
Wow, that is a complicated tutorial for this guy (I'm not much of a developer). If I moved the css rule to the php file itself, would this be required? I had tried to add the css rule inside a style="" tag but that didn't work either. –  Adam Nov 14 '11 at 2:56
    
The problem with this answer is that all CSS files will be pre-processed and none will be cached. –  Doug Nov 14 '11 at 15:07
add comment

You have several different problems. One is that your CSS selector (.rss a) doesn't refer to anything in your HTML. You want a.rss instead.

Second, as previously stated, CSS files are not parsed by PHP (by default, anyway), so you need your favicon reference somewhere that gets parsed by PHP.

Also, since each feed will have a different favicon, you need to fetch the correct one within your foreach loop. Try something like this (unnecessary line-breaks added for clarity):

foreach ( $rss_items as $item ) : ?>
    <li>   
        <a class='rss' 
            style='background-image:<?php echo $item->get_favicon(); ?>'
            href='<?php echo esc_url( $item->get_permalink() ); ?>' 
            title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'
        >
            <?php echo esc_html( $item->get_title() ); ?>
        </a>
    </li>
<?php endforeach; ?>

And here's your CSS:

a.rss {
    padding:0 0 0 20px;
    background:transparent url('some-placeholder-image-in-case-no-favicon-exists.png') no-repeat 0 1px;
}

Also -- and this is just editorializing, not related to your questions -- don't add an <hr> to the <li>; use CSS to add a bottom border.

share|improve this answer
    
Hi Doug, thanks for the response. I made all the changes but there is still something wrong with get_favicon() in how we're trying to use it. If I remove it, everything works fine. If I leave it, the page stops loading after the initial <li> tag. [Mon Nov 14 11:18:28 2011] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined method SimplePie_Item::get_favicon() in C:\\xampp\\htdocs\\wordpress\\wp-content\\themes\\beernews-child-theme\\single-city.php on line 92 –  Adam Nov 14 '11 at 16:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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