Custom Fields in your Feed

Since I work very often with custom fields and integrate various functions in my theme, I noticed something which I use extensively. I fill my feed exclusively with content from the custom fields.
For example, a short additional text (thumbnails, information, links, etc.):

function feed_monkey( $content ) {
    global $post, $id;

    if ( !is_feed() )
        return $content;

    $attachment = get_post_meta( $post->ID, 'Attachment', $single = true );

    // Print custom fields ***
    if ( $attachment != '' )
        return __('more information: ') . $attachment;

add_filter( 'the_content', 'feed_monkey' );

It works for me with a whole series of custom fields. With this solution, only the content of the custom fields are included in the feed, not the content of the actual content, very useful for photoblogs.

To attach the content of the custom fields to the actual content, you must output the variable $content:

    // Print custom fields and Content
    if ( $attachment != '' )
        return $content . '
' . __('Additionalinformation: ') . $attachment;

Guest Post

This post is written by Matthias Steiger and is a post in our Advent Calendar on WP Engineer about WordPress.
Thank you very much from my part to Matthias (Nicki).
If you also like to have your interesting post published on our website, please let us know on our contact page. We will of course appreciate your contribution!





4 responses to “Custom Fields in your Feed”

  1. Tom Avatar

    Also entweder $anhang oder $attachment. So, wie es jetzt da oben steht, gehts bestimmt nicht. 😉

  2. Michael Avatar

    Danke Tom! Ist korrigiert.

  3. Bajki Avatar

    good job, thanks for that tip 🙂
    But, have you any idea on how to modify feed field ?

  4. thomasgalvin Avatar

    Really impressive post Nicki!!
    I hope you don’t mind may i calling you Nicki.