Support » Fixing WordPress » Writing a filter to add text to the_content

  • Resolved chrisanthropic

    (@chrisanthropic)


    I’ve been reading up on filters and adding text to the content but I’ve yet to figure out my situation.

    What I’d like to do is have this displayed before every posts content:

    Title: the_title();
    Author: get_post_meta($post->ID, 'wpcf-book_author', true);
    Genre:  the_tags();
    Price: get_post_meta($post->ID, 'wpcf-book-price', true);

    I can manage a simple filter that displays text, but I can’t figure out displaying text plus the results of a function. Any pointers would be helpful and appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • basic example:

    add_filter('the_content','add_postdata_to_content');
    function add_postdata_to_content($text) {
    global $post;
      $postdata = 'Title: ' . get_the_title($post->ID) . '<br />';
      $postdata .= 'Author: ' . get_post_meta($post->ID, 'wpcf-book_author', true) . '<br />';
      $postdata .= 'Genre: ' . get_the_tag_list('',', ','') . '<br />';
      $postdata .= 'Price: ' . get_post_meta($post->ID, 'wpcf-book-price', true) . '<br />';
    return $postdata . $text;
    }

    http://codex.wordpress.org/Function_Reference/get_the_title
    http://codex.wordpress.org/Function_Reference/get_the_tag_list

    Thread Starter chrisanthropic

    (@chrisanthropic)

    Thanks for that, I’m still new to php and I was screwing up the single quotes and struggling with the . for stringing things together.

    It works, and it displays what I wanted (Thanks!) now is there any way to actually “add” it to the content so that it shows up in an RSS feed?

    Thread Starter chrisanthropic

    (@chrisanthropic)

    Ok, the theme I’m using does some weird stuff in/out of the loop but I was able to get it to work by putting the code in my functions.php file instead of my template directly. Thank you very much for the help.

    I had to change $text to $content to get the content to display and now everything is displayed properly. I’m having trouble getting my RSS to display the breaks but that’s a different issue alltogether.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Writing a filter to add text to the_content’ is closed to new replies.