WordPress.org

Ready to get started?Download WordPress

Forums

Using wp_list_categories() to generate page of RSS feeds (10 posts)

  1. pinkbeats
    Member
    Posted 4 years ago #

    G'day WordPress code monkeys, hope you can help or point me in the right direction :)

    I've wanted to set up a way of allowing folk to subscribe to individual category feeds on my blog as list on a page. To achieve that, I used information from this WordPress page in the Codex in conjunction with the Exec-PHP plugin so the php code I used in the page would work.

    The page has been published and is viewable here.

    I would be grateful if anyone can tell me how I can display the category names without the hyperlinks to the categories themselves? The parameters on the codex page don't seem to allow showing the categories without them being urls. The code I have used is:
    <?php wp_list_categories('orderby=name&feed_image=http://tiptopmusic.com/pinkblog/wp-includes/images/rss.png&show_count=1'); ?>

    Also the RSS icon on my page is annoyingly surrounded by a grey box that changes to red when the cursor hovers over. I realise this can be amended by CSS but I'm not sure where it's being inherited from and would rather it didn't display. Thanks.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    You might be better off using get_all_category_ids to grab all categories. Then using a foreach loop and get_cat_name to generate each category title with get_category_feed_link providing the feed link itself.

    <?php
    $all_cats = get_all_category_ids();
    $feed_list =array();
    if($all_cats) {
    	foreach ($all_cats as $this_cat) {
    		$cat_title = get_cat_name($this_cat);
    		$cat_feed = get_category_feed_link($this_cat);
    		$feed_list[$cat_title] = $cat_feed;
    	}
    	ksort($feed_list); // puts list in alphabetic order by title
    	echo '<ul>';
    	foreach($feed_list as $feed_title =>$feed_link) {
    		echp '<li>'.$feed_title.' <a href="'.$feed_link.'"><img src="'.bloginfo('template_directory')/images/rss.png.'" width="32" height="32" alt="RSS 2 feed for '.$feed_title.'" /></a></li>';
    	}
    	echo '</ul>';
    }
    ?>

    The above hasn't been tested, though, so apologies in advance for any syntax errors. Plus it assumes that the RSS icon is in your current theme's images directory (which might be better that using an image from within the wp-includes directory).

  3. pinkbeats
    Member
    Posted 4 years ago #

    Hey - thanks for the code.
    OK, tested it out and, yep, I've got a syntax error:

    syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

    which I'm trying to figure out now...

    I moved the rss icon images to the images folder for my theme as well.

  4. pinkbeats
    Member
    Posted 4 years ago #

    Just spotted a typo - the penultimate echo is mis-typed as echp... it kinda then half-works in that every category has the following error displayed on screen:

    Warning: Division by zero in /mydomain/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 13

    Doing some googling...

  5. pinkbeats
    Member
    Posted 4 years ago #

    Thanks for the code suggestion it's working but for this "Division by zero" error for each category and I'm not sure what's the cause.

  6. pinkbeats
    Member
    Posted 4 years ago #

    OK I got some help on another forum where the following syntax error was pointed out:

    "'.bloginfo('template_directory')/images/rss.png.'"

    changed to

    "'.bloginfo('template_directory'). '/images/rss.png"

    That gets rid of the error messages and it's pretty much all working except that before each category, I get the path to my theme each time and the image isn't showing e.g.:

    etc.

  7. pinkbeats
    Member
    Posted 4 years ago #

    I've got around that previous error by inserting the absolute URL to where the image is over the bit
    '.bloginfo('template_directory'). '/images/rss.png"
    that displayed the image and stopped printing the path to my theme each time; if you know the correct way to insert the path without using an absolute URL, lemme know.

    It's looking good - final two things that I'd like to tweak are:

    1. to sort the categories alphabetically without separating out those categories that start with a capital letter as it is currently
    2. to make the RSS icon appear before the category name

    Cheers.

  8. pinkbeats
    Member
    Posted 4 years ago #

    Just solved 1 - replaced ksort with natcasesort.

  9. pinkbeats
    Member
    Posted 4 years ago #

    Solved 2. as well, final code looked like this:

    <?php
    $all_cats = get_all_category_ids();
    $feed_list =array();
    if($all_cats) {
    	foreach ($all_cats as $this_cat) {
    		$cat_title = get_cat_name($this_cat);
    		$cat_feed = get_category_feed_link($this_cat);
    		$feed_list[$cat_title] = $cat_feed;
    	}
    	natcasesort($feed_list); // puts list in alphabetic order by title
    	echo '<ul>';
    	foreach($feed_list as $feed_title =>$feed_link) {
    		echo '<li><a href="'.$feed_link.'"><img src="http://mydomain.com/blog/wp-content/themes/themename/images/rss.png" width="14" height="14" alt="RSS 2 feed for '.$feed_title.'" /></a> '.$feed_title.'</li>';
    	}
    	echo '</ul>';
    }
    ?>

    Lastly, just need to fiddle around with the CSS to get rid of the border around the RSS icon and it will be complete. Thanks for your help.

  10. pinkbeats
    Member
    Posted 4 years ago #

    Switched back to ksort as natcasesort didn't actually quite work as those categories with child categories were grouped together; may do some further research on the web on sorting arrays but I'll leave it as ksort for the moment.

Topic Closed

This topic has been closed to new replies.

About this Topic