Custom Fields on RSS feed: how to?
-
HI
I’m looking for a way to add custom field to my Rss feed, I found many plugin but nothing’s working.
is there a solution? for exemple to create a function to avoid modifying the feed-rss2.php file.
thanks
-
it’s not working, is it because i’m usin excerpt not full content on my rss?
i’ll try it on an other blog to see.
thanks anywayWithout an existing plug-in that works for you, modifying the wp-includes/feed-rss2.php is not that big a deal. Add something like:
<nww:source> <?php if (get_post_custom_values('Source')) : foreach (get_post_custom_values('Source') as $sourcename) { echo $sourcename; } else : echo '~'; endif; ?> </nww:source>
Note that in this case, I know that I will always have only one value, but if you have more, I think you need to put the enclosing custom tags <nww:source>…</nww:source> inside the foreach loop to enclose each value separately (then you could dispense with the “else” statement, too. “Source” in this example is, of course, my custom field.
As far as I know, rss readers will generally ignore this tag. I added it for a newsletter, for which the xml parser does in fact read it (and for which reason I put in a “~” instead of not writing the tag if the custom field doesn’t exist — because the newsletter expects it).
Since this is creating a nonstandard rss tag, you also have to provide a the namespace file (for “nww” in this case) in the <rss> code near the top:
xmlns:nww="https://www.thedomain.org/namespace.php"
????? ?? ????? ?????? ??
ericr23: Wow, that’s really useful.
Here’s my question for you: I currently have my site (https://shortformblog.com/) set up with an RSS feed designed from a hacked plugin that uses an array call and a bunch of different styles based on the custom field. It’s worked OK so far but occasionally things come in out of order. And right now, for no good reason, everything’s coming out in reverse order.
I want to hardcode the order using a bunch of if-then-else statements, like I have it set up on my blog. The method you use … can it be used in such a fashion.
To give you an idea, here’s how the code is hacked up at the moment. I left a couple of things out; this is merely to show how I break up code currently:
// $pass_keys - Array of allowed custom keys. Modify to your needs. $pass_keys = array('bignumber', 'bignumberwords', 'bignumberb', 'bignumberwordsb', 'quote', 'attribution', 'factboxhed', 'factbox', 'factboxhedb', 'factboxb', 'factboxhedc', 'factboxc', 'grabbag', 'grabbagb', 'grabbagc', 'grabbagd', 'grabbage', 'tightcrop', 'cutline', 'embed', 'cutlinefull', 'source');
function szub_line_mode($key, $value, $sep, $list_mode) { switch( $list_mode ) { case 'p': if ($key == 'quote'){ echo '<h1 style="line-height:normal; font:bold italic;">“' . $value . '” </h1>';} elseif(in_array($key, array('bignumber', 'bignumberb'))){ echo '<h1 style="line-height:normal; color:#c00c20;">' . $value . ' </h1>';} elseif($key == 'tightcrop'){ echo '<img src="' . $value . '"> ';} elseif(in_array($key, array('factboxhed', 'factboxhedb', 'factboxhedc'))){ echo '<h3 style="color:#c00c20;">' . $value . ' </h3>';} elseif($key == 'grabbag'){ echo '<h2 style="line-height:normal; color:#c00c20;">One</h2><p>' . $value . '</p>';} elseif($key == 'grabbagb'){ echo '<h2 style="line-height:normal; color:#c00c20;">Two</h2><p>' . $value . '</p>';} elseif($key == 'grabbagc'){ echo '<h2 style="line-height:normal; color:#c00c20;">Three</h2><p>' . $value . '</p>';} elseif($key == 'grabbagd'){ echo '<h2 style="line-height:normal; color:#c00c20;">Four</h2><p>' . $value . '</p>';} elseif($key == 'grabbage'){ echo '<h2 style="line-height:normal; color:#c00c20;">Five</h2><p>' . $value . '</p>';} elseif($key !== 'source'){ echo '<p>' . $value . ' </p>';} elseif($key == 'source'){ echo ' <a href="' . $value . '" target="_blank">Source</a> ';} elseif($key !== ''){ echo '<p>' . $value . ' </p>';} } } add_filter('the_content', 'szub_custom2feed', 9);
And to give you an idea, this is what I do with the code on the frontend of the site:
<?php //Get source $source = get_post_meta($post->ID, 'source', $single = true); ?> <?php // PULL QUOTE // Get quote $quote = wptexturize(get_post_meta($post->ID, 'quote', $single = true)); // Get attribution $attribution = wptexturize(get_post_meta($post->ID, 'attribution', $single = true)); // If post is in a series if($quote !== '' & $attribution !== '' & $source == '') { // Echo the post style echo '<div class="quotebox"> <p class="pullquote">' . $quote . '</p> <br /> <p class="attribution">'. $attribution . '</p> </div>';} else if($quote !== '' & $attribution !== '' & $source !== '') { // Echo the post style echo '<div class="quotebox"> <p class="pullquote">' . $quote . '</p> <br /> <p class="attribution">'. $attribution . ' • <a class="source" href="' . $source . '" target="_blank">source</a> </p> </div>';} ?>
Is this possible to duplicate (or at least equivocate) in RSS?
The RSS feed is essentially just a custom strict XML index page. So you should indeed be able to duplicate your code in it. Nice looking site!
Hi Eric,
I actually finally figured it out. I made a similar data loop for the RSS feed using a hacked version of the Tadlock plugin.
https://justintadlock.com/archives/2008/01/27/custom-fields-for-feeds-wordpress-plugin
The feed looks great now. I’m quite happy with it.
Thanks for the help and the kind words!
Hey…
have tried it all….cant get this to work!!!
anyone that can provide an easy step-by-step for making this possible ???
all i want is to show custom fields value in rss…..they dont have to show image or so…..just the image url….and other custom fields value
is this possible….if it is…..please make an easy step by step
that would help alot of people….
One problem you may be finding is that most rss readers ignore such custom fields. So it doesn’t matter what extras you put into your feed except when you also control the reading of it.
- The topic ‘Custom Fields on RSS feed: how to?’ is closed to new replies.