0

I am currently working on a plugin that creates a custom post type. The post type has various meta fields which are used to display the content. I am struggling trying to determine how to display the meta fields inside the existing theme on the site.

I know HOW to display meta fields and that's not the issue so much. Right now when you go to the post created by the custom post type it just displays the Title, but not the meta fields. How do I append my fields from within the plugin? This needs to work across all themes so I can't just create a template and then force it upon the user.

This is what I currently have, but it displays the output before the content and not in the content area:

/* Filter the single_template with our custom function*/
add_filter('single_template', 'om_gallery_template');

function om_gallery_template($single) {
    global $wp_query, $post;

    /* Checks for single template by post type */
    if ($post->post_type == "searchable-media"){
       // if(file_exists(plugin_dir_path(__FILE__). '/single-searchable-media.php'))
       //     return plugin_dir_path(__FILE__) . '/single-searchable-media.php';
        $metas = get_post_custom($post->ID);
        $media_description = isset( $metas['media_description'] ) ?  $metas['media_description'][0]  : '';
        $content = get_the_content() . " $media_description. " ;
        echo apply_filters('the_content',$content);
    }
    //return $single;

I also tried setting the apply filter line to $single and returning but then I get an error:

Warning: include(

Tanner had a screaming monkey toy that would shriek when you banged it. He was very good at mimicking it..

): failed to open stream: No such file or directory in /home/myplugin/public_html/wp-includes/template-loader.php on line 74

1

Template filters have to return the name of a template file, not content. Their purpose is to override template selection with a different template.

If you want to modify the content output within the content of a template, then you need to add a filter to the_content.

function my_the_content_filter( $content ) {
    global $post;
    if ($post->post_type == "searchable-media"){
        // your code to append your meta fields to $content
    }
    return $content;
}

add_filter( 'the_content', 'my_the_content_filter' );
  • I was just coming to answer this with the same thing, I realized that I was using the wrong filter. Thank you! – plumwd Jul 29 '14 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.