WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Writing a filter to add text to the_content (4 posts)

  1. chrisanthropic
    Member
    Posted 1 year ago #

    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.

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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

  3. chrisanthropic
    Member
    Posted 1 year ago #

    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?

  4. chrisanthropic
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic